英文:
how can full clear focus in editText , just like press button DONE on softkeyboard
问题
我在编辑文本焦点变化时指定不同的背景,但是 clearFocus()
似乎没有效果,因为背景没有变成正确的状态。
英文:
I specify different backgrounds when edit text focus changed , but clearFocus()
seem like no effect , because the background do not change to the right state .
答案1
得分: 2
创建一个额外的EditText(edit_hidden)并将其设置为GONE或INVISIBLE
当你想要清除EditText(edit_your)的焦点时,使用以下代码
edit_your.clearFocus();
edit_hidden.requestFocus();
使用Action Done事件
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;
}
});
英文:
Create one more EditText(edit_hidden) and set visibility GONE or INVISIBLE
When you want to clear Focus from your EditText(edit_your) then use this Code
edit_your.clearFocus();
edit_hidden.requestFocus();
Using Action Done Event
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;
}
});
答案2
得分: 0
首先,我们可以使用 cleareFocus
方法来移除焦点。
editTextView.clearFocus()
但是当调用这个方法时,正如源代码注释所说:
当不处于触摸模式时,框架将尝试在清除焦点后将焦点给予从顶部开始的第一个可获取焦点的视图。因此,如果此视图是从顶部开始可以获取焦点的第一个视图,那么在调用它后,与清除焦点相关的所有回调都将在框架将焦点给予此视图之后被调用。
所以在你调用它之后,第一个元素将保持获得焦点,因此我们应该在父布局中将 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"
/>
英文:
Firstly, we can use cleareFocus
method to remove the focus.
editTextView.clearFocus()
But when this method is called, as source code comment says:
> 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.
so after you call it, the first element will stay get the focus. so we should set the touch-mode
true in parent layout.
<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"
/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论