make editText失去了对背压的关注

时间:2012-12-20 15:09:01

标签: android android-edittext

在我的活动中,我有一个editText字段。当用户点击它时,editText获得焦点并出现键盘。现在,当用户按下电话上的硬件后退按钮时,键盘消失,但光标仍保留在Edittext中,i。例如,它仍然是焦点。按下后退按钮时是否可以使EditText失去焦点?我尝试使用以下代码,但它不起作用:

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

6 个答案:

答案 0 :(得分:15)

只需扩展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 ( event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP )
            this.clearFocus();

        return super.onKeyPreIme( key_code, event );
    }
}

在xml中只使用<yourPackage.EditTextV2>代替<EditText>

注意:根据您支持的最小API,您可能需要向此类添加/删除构造函数。我建议只添加它们并删除那些super()调用以红色加下划线的那些。

答案 1 :(得分:2)

您可以让Views的另一个人专注于焦点,例如ImageView。请务必使用setFocusableInTouchMode(true)并在onResume()View使requestFocus()View,使其在触摸模式下可对焦。

您还可以创建一个0维的虚拟{{1}}并执行上述相同的步骤。

我希望这会有所帮助。

答案 2 :(得分: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);

答案 3 :(得分:0)

对于使用 Kotlin Material 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)
    }
}

答案 4 :(得分: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,这解决了我的问题,即从编辑文本中删除焦点并隐藏软输入

答案 5 :(得分:-1)

这可能是一种解决方案:

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;
        }
    });