Android 使editText在后按时失去焦点

Android 使editText在后按时失去焦点,android,android-edittext,Android,Android Edittext,在我的活动中,我有一个editText字段。当用户点击它时,编辑文本获得焦点,键盘出现。现在,当用户按下手机上的hardware back(硬件后退)按钮时,键盘消失,但光标仍保留在编辑文本中,即。e、 ,它仍然有焦点。按下后退按钮时是否可能使编辑文本失去焦点?我尝试使用以下代码,但无效: @Override public void onBackPressed() { vibrator.vibrate(Constants.DEFAULT_VIBRATE_TIME); myEdit

在我的活动中,我有一个editText字段。当用户点击它时,编辑文本获得焦点,键盘出现。现在,当用户按下手机上的hardware back(硬件后退)按钮时,键盘消失,但光标仍保留在编辑文本中,即。e、 ,它仍然有焦点。按下后退按钮时是否可能使编辑文本失去焦点?我尝试使用以下代码,但无效:

@Override
public void onBackPressed() {
    vibrator.vibrate(Constants.DEFAULT_VIBRATE_TIME);
    myEditText.clearFocus();
            super.onBackPressed();
}

您可以使另一个
视图
可聚焦,例如
图像视图
。确保在触摸模式下,使用
setFocusableInTouchMode(true)
onResume()
将其调焦至
View
requestFocus()

此外,您还可以创建具有0个维度的虚拟
视图
,并执行上述相同步骤


我希望这会有所帮助。

添加视图,例如比编辑文本更高的视图:

<LinearLayout
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />
public class EditTextV2 extends EditText
{
    public EditTextV2( Context context )
    {
        super( context );
    }

    public EditTextV2( Context context, AttributeSet attribute_set )
    {
        super( context, attribute_set );
    }

    public EditTextV2( Context context, AttributeSet attribute_set, int def_style_attribute )
    {
        super( context, attribute_set, def_style_attribute );
    }

    @Override
    public boolean onKeyPreIme( int key_code, KeyEvent event )
    {
        if ( key_code == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP )
            this.clearFocus();

        return super.onKeyPreIme( key_code, event );
    }
}
只需扩展EditText:

<LinearLayout
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />
public class EditTextV2 extends EditText
{
    public EditTextV2( Context context )
    {
        super( context );
    }

    public EditTextV2( Context context, AttributeSet attribute_set )
    {
        super( context, attribute_set );
    }

    public EditTextV2( Context context, AttributeSet attribute_set, int def_style_attribute )
    {
        super( context, attribute_set, def_style_attribute );
    }

    @Override
    public boolean onKeyPreIme( int key_code, KeyEvent event )
    {
        if ( key_code == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP )
            this.clearFocus();

        return super.onKeyPreIme( key_code, event );
    }
}
在xml中,只需使用
而不是


注意:您可能需要向此类添加/删除构造函数,具体取决于您支持的最小API。我建议将它们全部添加,并删除那些其
super()
调用以红色下划线的调用。

这可能是一个可行的解决方案:

EditText et;
et.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if(i == KeyEvent.KEYCODE_BACK) {
                et.clearFocus();
                return true;
            }
            else return false;
        }
    });

对于使用Kotlin材料设计的任何人,您可以使用以下内容:

class ClearFocusEditText: TextInputEditText {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun onKeyPreIme(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            clearFocus()
        }

        return super.onKeyPreIme(keyCode, event)
    }
}

我提供了kotlin等价的代码来编辑Kacy的答案

class CustomSearch @JvmOverloads constructor(context: Context, attrs: AttributeSet? = 
null, defStyle: Int = 0):AppCompatEditText(context, attrs, defStyle) {
override fun onKeyPreIme(keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK){
    val imm:InputMethodManager = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0)
    this.clearFocus()
    //this.findFocus()
}
return true
}

注意:我返回true,这解决了我的问题,即从编辑文本中删除焦点并隐藏软输入

确保myEditText不是布局中的第一个输入元素。如果是,则首先从编辑文本中删除焦点,然后将焦点放在任何其他元素上,如TextView etcIt是我活动中的第一个输入元素。我尝试了
title.requestFocus()
,但首先使title可聚焦,然后调用title.requestFocus(),它仍然没有失去焦点。您可以使用它来隐藏键盘,而无需按下后退按钮(我知道您需要更多btu,这仍然很重要):this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u STATE\u ALWAYS\u HIDDEN);如果要使用构造函数:
EditText(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes)
最低API要求是完美的解决方案。当您需要使用
查看.OnKeyListener
使用片段覆盖后退按钮行为,并且需要捕获后退按钮单击事件,而焦点文本中断您的实现时,此功能非常有用。干杯!