簡體   English   中英

如何停止編輯文本以將焦點更改為單擊片段中的后退按鈕?

[英]How to stop the edittext to change the focus on the click of the back button in fragment?

當用戶單擊“后退”按鈕時,我想清除edittext的焦點。

這是我的onResume方法:

public void onResume() {
        super.onResume();

        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (i == keyEvent.KEYCODE_BACK) {
                    mEdtDob.clearFocus();
                    return true;
                }
                return false;

            }
        });

    }

我想這樣做,當我單擊后退按鈕時,不應更改edittext的焦點。

解決方案:代碼正確。 檢查EditText的父視圖是否可單擊? 如果不是,則在刪除焦點后,EditText可能會再次獲得焦點。

將此行添加到xml 中EditText的父視圖中

android:focusable="true"
android:focusableInTouchMode="true"
@Override public void onBackPressed() { 
int count =    getFragmentManager().getBackStackEntryCount(); 
if (count == 0) { 
super.onBackPressed();
mEdtDob.clearFocus();
//additional code 
} else       {     getFragmentManager().popBackStack(); 
} }

檢查此鏈接:

如何在片段中實現onBackPressed()?

只需在onKey Method.Code中添加這兩行:

mEdtDob.setFocusable(true);
mEdtDob.setFocusableInTouchMode(true);

如果這不起作用,請在評論中讓我知道...。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM