Android : clear focus on edittext when activity starts

跟風遠走 提交于 2020-01-10 19:32:11

问题


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.

<RelativeLayout 
     android:id="@+id/RequestFocusLayout"
     android:focusable="true"
     android:focusableInTouchMode="true"
     android:layout_width="0px"
     android:layout_height="0px"/>

and in java code

 RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
 focuslayout.requestFocus();

The above code is working fine when activity starts at first time and if same activity starts again, automatically edittext get focus.

Can anyone help me to solve this issue.


回答1:


If Come back from other activity edittext get focused. 

put these line onStart() or on onReusme()

  RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
 focuslayout.requestFocus();



回答2:


Actually, the first focusable view in the activity receives initial focus. If that happens to be your EditText, it will be initially focused.

If you don't want that, here's your options:

Focus another view

Programmatically identify what view you do want to give initial focus to.

@Override
protected void onStart() {

    super.onStart();
    findViewById( R.id.yourOtherViewId ).requestFocus();
}

Make an earlier view in your layout focusable

If you rather it appears as though "no view has initial focus" you could make the parent view group focusable. In the following example, I make my LinearLayout focusable by setting android:focusableInTouchMode="true":

<LinearLayout
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        ...



回答3:


If your EditText is a child of your RelativeLayout you can use android:descendantFocusability to request the focus:

<RelativeLayout 
     android:id="@+id/RequestFocusLayout"
     android:focusable="true"
     android:layout_width="0px"
     android:layout_height="0px" 
     android:descendantFocusability="beforeDescendants" 
     android:focusableInTouchMode="true" />



回答4:


I used the solution shown up in this page but it didn't work. Add this attribute to your activity tag into AndroidManifest:

android:windowSoftInputMode="stateHidden"

It works perfectly.




回答5:


Me realy help only android:focusableInTouchMode="true" in my parent view group.




回答6:


Put the code to remove the focus in your onStart() method and it should work fine.




回答7:


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




回答8:


If on starting the Activity it focuses on EditText on the screen. use

android:focusableInTouchMode="true"

in the parent containing that EditText. Like if EditText is inside a RelativeLayout use this line in the RelativeLayout.



来源:https://stackoverflow.com/questions/11157370/android-clear-focus-on-edittext-when-activity-starts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!