How to stop EditText from gaining focus at Activity startup in Android

后端 未结 30 2714
别那么骄傲
别那么骄傲 2020-11-21 06:40

I have an Activity in Android, with two elements:

  1. EditText
  2. ListView

When my Activity

相关标签:
30条回答
  • 2020-11-21 07:12

    A simpler solution exists. Set these attributes in your parent layout:

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

    And now, when the activity starts this main layout will get focus by default.

    Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:

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

    Good comment from Guillaume Perrot:

    android:descendantFocusability="beforeDescendants" seems to be the default (integer value is 0). It works just by adding android:focusableInTouchMode="true".

    Really, we can see that the beforeDescendants set as default in the ViewGroup.initViewGroup() method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;

    Thanks to Guillaume.

    0 讨论(0)
  • 2020-11-21 07:12

    Late but simplest answer, just add this in parent layout of the XML.

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

    Upvote if it helped you ! Happy Coding :)

    0 讨论(0)
  • 2020-11-21 07:13

    Try clearFocus() instead of setSelected(false). Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.

    0 讨论(0)
  • 2020-11-21 07:13

    Try this before your first editable field:

    <TextView  
            android:id="@+id/dummyfocus" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="@string/foo"
            />
    
    ----
    
    findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
    findViewById(R.id.dummyfocus).requestFocus();
    
    0 讨论(0)
  • 2020-11-21 07:14

    None of this solutions worked for me. The way I fix the autofocus was:

    <activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
        <intent-filter >
        </intent-filter>
    </activity>
    
    0 讨论(0)
  • 2020-11-21 07:15

    Write this code inside Manifest file in the Activity where you do not want to open the keyboard.

    android:windowSoftInputMode="stateHidden"
    

    Manifest file:

     <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.projectt"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="24" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
             <activity
                android:name=".Splash"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".Login"
                **android:windowSoftInputMode="stateHidden"**
                android:label="@string/app_name" >
            </activity>
    
        </application>
    
    </manifest>
    
    0 讨论(0)
提交回复
热议问题