如何完全清除 editText 中的焦点,就像按下软键盘上的 DONE 按钮一样
how can full clear focus in editText , just like press button DONE on softkeyboard
我在编辑文本焦点改变时指定了不同的背景,但是clearFocus()
好像没有效果,因为背景没有改变到正确的状态。
再创建一个 EditText(edit_hidden) 并设置可见性 GONE 或 INVISIBLE
当您想从 EditText(edit_your) 中清除焦点时,请使用此代码
edit_your.clearFocus();
edit_hidden.requestFocus();
使用动作完成事件
edit_your.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
edit_your.clearFocus();
edit_hidden.requestFocus();
}
return false;
}
});
首先,我们可以使用cleareFocus
方法来移除焦点。
editTextView.clearFocus()
但是当这个方法被调用时,正如源码注释所说:
When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. Hence, if this
View is the first from the top that can take focus, then all callbacks
related to clearing focus will be invoked after which the framework will
give focus to this view.
所以在你调用它之后,第一个元素将保持焦点。所以我们应该在父布局中设置 touch-mode
true。
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<EditText android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
我在编辑文本焦点改变时指定了不同的背景,但是clearFocus()
好像没有效果,因为背景没有改变到正确的状态。
再创建一个 EditText(edit_hidden) 并设置可见性 GONE 或 INVISIBLE
当您想从 EditText(edit_your) 中清除焦点时,请使用此代码
edit_your.clearFocus();
edit_hidden.requestFocus();
使用动作完成事件
edit_your.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
edit_your.clearFocus();
edit_hidden.requestFocus();
}
return false;
}
});
首先,我们可以使用cleareFocus
方法来移除焦点。
editTextView.clearFocus()
但是当这个方法被调用时,正如源码注释所说:
When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after which the framework will give focus to this view.
所以在你调用它之后,第一个元素将保持焦点。所以我们应该在父布局中设置 touch-mode
true。
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<EditText android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>