我有一个编辑文本,它需要像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();
}
}
还尝试覆盖键向下事件。
@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);
}
如何创建 EditText 的子类并覆盖其 onKeyPreIme 方法
/**
* 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);
}
}