Edittext删除焦点,并且不将焦点转移到其他edittext上

时间:2018-07-19 03:39:06

标签: android android-edittext android-textinputlayout

我认为我有2个EditText

当我在上一个EditText上并在软键盘上按完成时。 焦点转移回我的第一个EditText,我觉得这很烦/错误。它应该只是清除焦点并隐藏键盘

这是我的xml:

   <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="4dp"
                    app:counterTextAppearance="@style/counterText"
                    app:counterOverflowTextAppearance="@style/counterOverride"
                    app:theme="@style/TextInputLayoutGold">

                    <android.support.design.widget.TextInputEditText
                        android:id="@+id/mMerchantName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:focusable="true"
                        android:textSize="@dimen/font_size"
                        android:inputType ="numberDecimal"
                        app:theme="@style/EditTextGold"
                        android:hint="@string/label_merchant_name"
                        />
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="4dp"
                    app:counterTextAppearance="@style/counterText"
                    app:counterOverflowTextAppearance="@style/counterOverride"
                    app:theme="@style/TextInputLayoutGold">

                    <android.support.design.widget.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:textSize="@dimen/font_size"
                        android:id="@+id/Transaction_amount"
                        android:inputType ="numberDecimal"
                        app:theme="@style/EditTextGold"
                        android:hint="@string/hint_transaction_amount"
                        />
                </android.support.design.widget.TextInputLayout>

3 个答案:

答案 0 :(得分:0)

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(EditText v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); View view = activity.getCurrentFocus(); if (view == null) { view = new View(activity); } imm.hideSoftInputFromWindow(view.getWindowToken(), 0); return true; } return false; } });

答案 1 :(得分:0)

您可以使用 clearFocus()清除 IME_ACTION_DONE 事件上的编辑文本焦点。

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE)
                {
                    previous_editText.clearFocus();
                    editText.clearFocus();
                    return true;
                }
                return false;
            }
        });

更新的代码:

还请在您的editText上添加 setOnFocusChangeListener

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus)
            {
                if(!hasFocus) 
                {
                    // Also check your condition becasue it will always called onfoucs change
                    // check your edit text has some entry or not 
                    previous_editText.clearFocus();
                }
            }
        });

答案 2 :(得分:0)

为您要关注的编辑文本添加此代码

edittext.setOnEditorActionListener(new OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
             final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        editText.requestFocus();
                    }
                }, 100);
        }    
        return false;
    }
});