25

在我的活动中,我有一个 editText 字段。当用户点击它时,editText 获得焦点并出现键盘。现在,当用户按下手机上的硬件返回按钮时,键盘消失,但光标仍留在 Edittext 中,即它仍然具有焦点。按下后退按钮时是否可以使 EditText 失去焦点?我尝试使用以下代码,但没有奏效:

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

6 回答 6

24

只需扩展 EditText:

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 中,只需使用<yourPackage.EditTextV2>而不是<EditText>.

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

于 2016-12-15T20:58:18.760 回答
3

对于使用KotlinMaterial Design的任何人,您都可以使用:

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)
    }
}
于 2019-04-26T06:26:11.707 回答
2

您可以使另一个Views焦点成为焦点,例如ImageView. 确保在触摸模式下使其可聚焦,使用setFocusableInTouchMode(true)和 on onResume()make that Viewto requestFocus()

您也可以创建一个View0 维的虚拟模型并执行上述相同的步骤。

我希望这有帮助。

于 2012-12-20T16:56:08.887 回答
0

添加比您的 EditText 更高的视图,如下所示:

<LinearLayout
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />

还要隐藏键盘在 onBackPressed() 中添加:

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
于 2012-12-21T06:21:41.023 回答
0

我正在通过一些编辑为 Kacy 答案提供 kotlin 等效代码

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,这解决了我的问题,即从编辑文本中移除焦点并隐藏软输入

于 2020-12-19T11:26:27.903 回答
-3

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

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;
        }
    });
于 2018-07-13T20:18:21.257 回答