How to remove focus from single editText

后端 未结 17 1424
南笙
南笙 2020-12-13 17:24

In my application I have a single EditText together with some TextViews, button and a spinner. My EditText receives focus since it is the only focu

相关标签:
17条回答
  • 2020-12-13 17:25

    Since I was in a widget and not in an activity I did:

    `getRootView().clearFocus();

    0 讨论(0)
  • 2020-12-13 17:25

    You only have to set the ViewGroup with the attribute:

    android:focusableInTouchMode="true"

    The ViewGroup is the layout that includes every child view.

    0 讨论(0)
  • 2020-12-13 17:27
    <EditText android:layout_height="wrap_content" android:background="@android:color/transparent" android:layout_width="match_parent" 
        android:clickable="false"
         android:focusable="false"
         android:textSize="40dp"
         android:textAlignment="center" 
        android:textStyle="bold"  
        android:textAppearance="@style/Base.Theme.AppCompat.Light.DarkActionBar" 
       android:text="AVIATORS"/>
    
    0 讨论(0)
  • 2020-12-13 17:28

    If I understand your question correctly, this should help you:

    TextView tv1 = (TextView) findViewById(R.id.tv1);
    tv1 .setFocusable(false);
    
    0 讨论(0)
  • 2020-12-13 17:29

    if Edittext parent layout is Linear then add

     android:focusable="true" 
     android:focusableInTouchMode="true"
    

    like below

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:focusable="true"
                android:focusableInTouchMode="true">
    
               <EditText/>
              ............
    

    when Edittext parent layout is Relative then

      android:descendantFocusability="beforeDescendants"
      android:focusableInTouchMode="true"
    

    like

      <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="beforeDescendants"
                android:focusableInTouchMode="true">
    
               <EditText/>
              ............
    
    0 讨论(0)
  • 2020-12-13 17:30

    I've tryed much to clear focus of an edit text. clearfocus() and focusable and other things never worked for me. So I came up with the idea of letting a fake edittext gain focus:

    <LinearLayout
        ...
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            ...
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        <!--here comes your stuff-->
    
        </LinearLayout>
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/fake"
            android:textSize="1sp"/>
    
    </LinearLayout>
    

    then in your java code:

    View view = Activity.this.getCurrentFocus();
                        if (view != null) {
                            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                            fake.requestFocus();
                        }
    

    it will hide the keyboard and remove the focus of any edittext that has it. and also as you see the fake edittext is out of screen and can't be seen

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