Android : clear focus on edittext when activity starts

前端 未结 8 950
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 14:30

I\'ve some settings page in my app. Once the activity gets starts directly it focus to edittext and i used following code to clear foucs.



        
8条回答
  •  失恋的感觉
    2021-01-02 14:51

    In the layout XML file, specify an imeOption on your EditText:

    android:imeOptions="actionGo"
    

    Next, add an action listener to your EditText in the Activity's java file

    mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                // hide virtual keyboard
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });
    

    Where mYourEditText is an EditText object

提交回复
热议问题