我需要在键盘关闭时更新我的Android应用程序的UI(使用后退按钮)。当我覆盖OnBackButtonPressed
时,它会像键盘一样触发,但不会在键盘启动时触发。是否有其他方法,或者我可以使用的各种听众?
答案 0 :(得分:0)
扩展edittext并使用以下代码:
public class EditTextBackEvent extends Edittext
{
private EditTextImeBackListener mOnImeBack;
public EditTextBackEvent(Context context) {
super(context);
}
public EditTextBackEvent(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextBackEvent(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
if (mOnImeBack != null) mOnImeBack.onImeBack(this, this.getText().toString());
}
return super.dispatchKeyEvent(event);
}
public void setOnEditTextImeBackListener(EditTextImeBackListener listener) {
mOnImeBack = listener;
}
public interface EditTextImeBackListener {
void onImeBack(EditTextBackEvent ctrl, String text);
}
}
然后你可以使用EdittetBackEvent.setOnEditTextImeBackListener()
答案 1 :(得分:0)
如果您想在用户输入信息后在表单中隐藏键盘,您可以执行的操作是隐藏键盘并在用户键入上一个editText后添加所需的行为:
EditText lastEditText = (EditText) findViewById(R.id.whatever);
lastEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
doWhateverYouWant();
}
}
});
隐藏键盘的方法:
private void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}