How to clear focus for EditText?

前端 未结 8 1099
滥情空心
滥情空心 2021-01-30 12:54

I have an activity with a search box (EditText) on the top, and a ListView below. Whenever the activity starts, the EditText always has focus and bring up the keybo

相关标签:
8条回答
  • 2021-01-30 13:34

    you must have mentioned <requestFocus> tag in your editTiext field in XML remove that and run again

    0 讨论(0)
  • 2021-01-30 13:38

    Set in your parent layout next attributes:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true" >
    

    And now, when activity starts this layout getting default focus.

    Also we can remove focus from children views in runtime (e.g. after finishing child editing):

    findViewById(R.id.mainLayout).requestFocus();
    

    or

    Look in the AndroidManifest.xml element.

    android:windowSoftInputMode="stateHidden"
    

    It always hide key board when entering the activity.

    0 讨论(0)
  • 2021-01-30 13:38

    You could use set focus on container view. Add to container android:focusableInTouchMode="true", android:focusable="true" and tag requestFocus example:

    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true"
        android:focusable="true">
        <requestFocus/>
    
        <EditText
            android:id="@+id/edit_text_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-01-30 13:38

    Add the android:focusable="true" and android:focusableInTouchMode="true" elements in the parent layout of EditText as follow;

    android:id="@+id/linearLayout7" android:layout_width="fill_parent"
    
    android:layout_height="wrap_content"
    
    android:focusable="true" android:focusableInTouchMode="true">
    

    I think, it should help you.

    0 讨论(0)
  • 2021-01-30 13:38

    This will do your work:

    android:windowSoftInputMode="stateHidden"

    0 讨论(0)
  • 2021-01-30 13:44

    You can try this:

    editText.clearFocus();
    

    where ever you want to clear focus.

    0 讨论(0)
提交回复
热议问题