在 Android 中检测到关闭键盘事件
Detect dismiss keyboard event in Android
我有一个 edittext 需要像 iOS 中的文本字段一样工作。当我点击它时,它应该变成可编辑的,否则它应该被禁用。假设用户想要编辑某个值,当他按下后退按钮时,随着键盘关闭,我希望编辑文本变得无法编辑。
我只使用了 setCursorVisible(true) 和 setCursorVisible(false) 方法。我确实尝试过使用 keyCode 事件,但它们没有帮助。这是我到目前为止尝试过的:
@Override
public void onBackPressed() {
// Had the InputMethodManager service here..
if(imm.isAcceptingText())
{
Toast.makeText(ProfileActivity.this,"working",Toast.LENGTH_SHORT).show();
}
else {
super.onBackPressed();
Toast.makeText(ProfileActivity.this,"back pressed called",Toast.LENGTH_SHORT).show();
}
}
还尝试覆盖 keyDown 事件。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
Log.d("123","called,called");
return true;
}
return super.onKeyDown(keyCode, event);
}
怎么样creating a subclass of EditText and overriding its onKeyPreIme method
/**
* This class overrides the onKeyPreIme method to dispatch a key event if the
* KeyEvent passed to onKeyPreIme has a key code of KeyEvent.KEYCODE_BACK.
* This allows key event listeners to detect that the soft keyboard was
* dismissed.
*
*/
public class ExtendedEditText extends EditText {
public ExtendedEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ExtendedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExtendedEditText(Context context) {
super(context);
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
dispatchKeyEvent(event);
return false;
}
return super.onKeyPreIme(keyCode, event);
}
}
我有一个 edittext 需要像 iOS 中的文本字段一样工作。当我点击它时,它应该变成可编辑的,否则它应该被禁用。假设用户想要编辑某个值,当他按下后退按钮时,随着键盘关闭,我希望编辑文本变得无法编辑。
我只使用了 setCursorVisible(true) 和 setCursorVisible(false) 方法。我确实尝试过使用 keyCode 事件,但它们没有帮助。这是我到目前为止尝试过的:
@Override
public void onBackPressed() {
// Had the InputMethodManager service here..
if(imm.isAcceptingText())
{
Toast.makeText(ProfileActivity.this,"working",Toast.LENGTH_SHORT).show();
}
else {
super.onBackPressed();
Toast.makeText(ProfileActivity.this,"back pressed called",Toast.LENGTH_SHORT).show();
}
}
还尝试覆盖 keyDown 事件。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//replaces the default 'Back' button action
if(keyCode==KeyEvent.KEYCODE_BACK)
{
Log.d("123","called,called");
return true;
}
return super.onKeyDown(keyCode, event);
}
怎么样creating a subclass of EditText and overriding its onKeyPreIme method
/**
* This class overrides the onKeyPreIme method to dispatch a key event if the
* KeyEvent passed to onKeyPreIme has a key code of KeyEvent.KEYCODE_BACK.
* This allows key event listeners to detect that the soft keyboard was
* dismissed.
*
*/
public class ExtendedEditText extends EditText {
public ExtendedEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ExtendedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExtendedEditText(Context context) {
super(context);
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
dispatchKeyEvent(event);
return false;
}
return super.onKeyPreIme(keyCode, event);
}
}