Android: Force EditText to remove focus?

This question already has an answer here:

  • Stop EditText from gaining focus at Activity startup 46 answers

  • You can add this to onCreate and it will hide the keyboard everytime the activty starts.

    You could also programatically change the focus to another item.

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    

    You can make cursor and focus disappear by

    edittext.clearFocus();
    

    But detect when the key board hide is a hard work.


    Add LinearLayout before EditText in your XML.

    <LinearLayout 
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:layout_width="0px"
        android:layout_height="0px" />
    

    Or you can do this same thing by adding these lines to view before your 'EditText'.

    <Button
        android:id="@+id/btnSearch"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:text="Quick Search"
        android:textColor="#fff"
        android:textSize="13sp"
        android:textStyle="bold" />
    
    <EditText
        android:id="@+id/edtSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:gravity="left"
        android:hint="Name"
        android:maxLines="1"
        android:singleLine="true"
        android:textColorHint="@color/blue"
        android:textSize="13sp"
        android:textStyle="bold" />
    
    链接地址: http://www.djcxy.com/p/24158.html

    上一篇: 不想让软键盘在启动时自动显示

    下一篇: Android:强制EditText删除焦点?