当按下软键盘中的“完成”按钮时,如何失去编辑文本的焦点?

时间:2012-08-16 06:26:13

标签: android android-edittext android-softkeyboard

我的xml中有7个edittext框。我正在使用OnFocusChangeListener从edittext读取值,我正在使用该值进行计算。我想让我的edittext在我点击软键盘中的完成按钮时失去焦点。所以我可以得到edittext中的值。

4 个答案:

答案 0 :(得分:59)

当从软键盘点击完成按钮时,调用clearFocus EditText方法失去焦点。这样做:

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //Clear focus here from edittext
             editText.clearFocus();
        }
    return false;
    }
});

并在edittext xml

中添加android:imeOptions="actionDone"

答案 1 :(得分:0)

如果有人遇到这个问题,想知道如何一次取消对所有内容的关注,他们可以将焦点放在父版式(或该版式的任何版式)上问题)。

findViewById(R.id.myLayout).requestFocus();

答案 2 :(得分:0)

更完整的答案

//  This will change the ‘RETURN’ button in your the EditText’s soft keyboard to a ‘DONE’ button.
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
//  Use the InputMethodManager to close the soft keyboard
editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //Clear focus here from edittext
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    return false;
    }
});

答案 3 :(得分:0)

科特琳,它对我有用:

main.clearFocus()

main是根ConstraintLayout

相关问题