如何在EditText中实现完全清晰的焦点,就像按软键盘上的DONE按钮一样。

huangapple go评论77阅读模式
英文:

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.

&lt;LinearLayout
    android:focusable=&quot;true&quot; 
    android:focusableInTouchMode=&quot;true&quot; 
    android:layout_width=&quot;0px&quot; 
    android:layout_height=&quot;0px&quot;/&gt;
&lt;EditText android:id=&quot;@+id/et&quot;
    android:layout_width=&quot;match_parent&quot; 
    android:layout_height=&quot;wrap_content&quot;
    /&gt;

huangapple
  • 本文由 发表于 2020年1月3日 15:32:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定