csdnceshi77
狐狸.fox
采纳率0%
2009-10-12 15:03

在活动启动时停止 EditText 获得焦点

已采纳

I have an Activity in Android, with two elements:

  1. EditText
  2. ListView

When my Activity starts, the EditText immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:

EditText.setSelected(false);

No luck. How can I convince the EditText to not select itself when the Activity starts?

转载于:https://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

29条回答

  • csdnceshi73 喵-见缝插针 12年前

    Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true" to <LinearLayout> like the following example will fix the problem.

    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_width="0px" 
        android:layout_height="0px"/>
    
    <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
    to prevent the dummy from receiving focus again -->
    <AutoCompleteTextView android:id="@+id/autotext"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:nextFocusUp="@id/autotext" 
        android:nextFocusLeft="@id/autotext"/>
    
    点赞 26 评论 复制链接分享
  • csdnceshi54 hurriedly% 11年前

    Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText? I don't really see an issue with the EditText having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText (and open the keyboard as a result).

    If it's the problem of the virtual keyboard, see the AndroidManifest.xml <activity> element documentation.

    android:windowSoftInputMode="stateHidden" - always hide it when entering the activity.

    or android:windowSoftInputMode="stateUnchanged" - don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).

    点赞 26 评论 复制链接分享
  • weixin_41568134 MAO-EYE 12年前

    The only solution I've found is:

    • Create a LinearLayout (I dunno if other kinds of Layout's will work)
    • Set the attributes android:focusable="true" and android:focusableInTouchMode="true"

    And the EditText won't get the focus after starting the activity

    点赞 20 评论 复制链接分享
  • csdnceshi77 狐狸.fox 10年前

    You can just set "focusable" and "focusable in touch mode" to value true on the first TextView of the layout. In this way when the activity starts the TextView will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...

    点赞 10 评论 复制链接分享
  • weixin_41568184 叼花硬汉 9年前

    The problem seems to come from a property that I can only see in the XML form of the layout.

    Make sure to remove this line at the end of the declaration within the EditText XML tags:

    <requestFocus />
    

    That should give something like that :

    <EditText
       android:id="@+id/emailField"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:inputType="textEmailAddress">
    
       //<requestFocus /> /* <-- without this line */
    </EditText>
    
    点赞 10 评论 复制链接分享
  • weixin_41568134 MAO-EYE 9年前

    I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.

    myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
    myLayout.setFocusableInTouchMode(true);
    

    That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.

    点赞 10 评论 复制链接分享
  • csdnceshi79 python小菜 9年前
    <TextView
        android:id="@+id/TextView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        style="@android:style/Widget.EditText"/>
    
    点赞 9 评论 复制链接分享
  • csdnceshi78 程序go 6年前

    Add following in onCreate method:

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    
    点赞 8 评论 复制链接分享
  • csdnceshi58 Didn"t forge 12年前

    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.

    点赞 7 评论 复制链接分享
  • csdnceshi65 larry*wei 10年前

    Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus() in onCreate()

    <EditText android:id="@+id/dummyEditTextFocus" 
    android:layout_width="0px"
    android:layout_height="0px" />
    

    That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).

    点赞 7 评论 复制链接分享
  • csdnceshi51 旧行李 9年前

    The following will stop edittext from taking focus when created, but grab it when you touch them.

    <EditText
        android:id="@+id/et_bonus_custom"
        android:focusable="false" />
    

    So you set focusable to false in the xml, but the key is in the java, which you add the following listener:

    etBonus.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.setFocusable(true);
            v.setFocusableInTouchMode(true);
            return false;
        }
    });
    

    Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.

    点赞 7 评论 复制链接分享
  • csdnceshi52 妄徒之命 12年前

    Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:

    <LinearLayout 'dummy'>
    <EditText et>
    
    dummy.setNextFocusDownId(et.getId());
    
    dummy.setNextFocusUpId(et.getId());
    
    et.setNextFocusUpId(et.getId());
    

    a lot of work just to get rid of focus on a view..

    Thanks

    点赞 7 评论 复制链接分享
  • csdnceshi75 衫裤跑路 10年前

    If you have another view on your activity like a ListView, you can also do:

    ListView.requestFocus(); 
    

    in your onResume() to grab focus from the editText.

    I know this question has been answered but just providing an alternative solution that worked for me :)

    点赞 6 评论 复制链接分享
  • weixin_41568174 from.. 8年前

    Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!

    public static View preventInitialFocus(final Activity activity)
    {
        final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
        final View root = content.getChildAt(0);
        if (root == null) return null;
        final View focusDummy = new View(activity);
        final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View view, boolean b)
            {
                view.setOnFocusChangeListener(null);
                content.removeView(focusDummy);
            }
        };
        focusDummy.setFocusable(true);
        focusDummy.setFocusableInTouchMode(true);
        content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
        if (root instanceof ViewGroup)
        {
            final ViewGroup _root = (ViewGroup)root;
            for (int i = 1, children = _root.getChildCount(); i < children; i++)
            {
                final View child = _root.getChildAt(i);
                if (child.isFocusable() || child.isFocusableInTouchMode())
                {
                    child.setOnFocusChangeListener(onFocusChangeListener);
                    break;
                }
            }
        }
        else if (root.isFocusable() || root.isFocusableInTouchMode())
            root.setOnFocusChangeListener(onFocusChangeListener);
    
        return focusDummy;
    }
    
    点赞 6 评论 复制链接分享
  • csdnceshi80 胖鸭 8年前

    The simplest thing I did is to set focus on another view in onCreate:

        myView.setFocusableInTouchMode(true);
        myView.requestFocus();
    

    This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.

    点赞 6 评论 复制链接分享
  • csdnceshi54 hurriedly% 8年前

    At onCreate of your Activity, just add use clearFocus() on your EditText element. For example,

    edittext = (EditText) findViewById(R.id.edittext);
    edittext.clearFocus();
    

    And if you want to divert the focus to another element, use requestFocus() on that. For example,

    button = (Button) findViewById(R.id.button);
    button.requestFocus();
    
    点赞 6 评论 复制链接分享
  • csdnceshi52 妄徒之命 10年前

    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();
    
    点赞 5 评论 复制链接分享
  • csdnceshi57 perhaps? 6年前

    Add android:windowSoftInputMode="stateAlwaysHidden" in the activity tag of the Manifest.xml file.

    Source

    点赞 5 评论 复制链接分享
  • weixin_41568208 北城已荒凉 5年前

    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>
    
    点赞 4 评论 复制链接分享
  • csdnceshi57 perhaps? 9年前

    I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.

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

    ( Reference from Silver https://stackoverflow.com/a/8639921/15695 )

    and remove

     <requestFocus />
    

    at EditText

    ( Reference from floydaddict https://stackoverflow.com/a/9681809 )

    点赞 4 评论 复制链接分享
  • weixin_41568131 10.24 7年前

    The following worked for me in Manifest. Write ,

    <activity
    android:name=".MyActivity"
    android:windowSoftInputMode="stateAlwaysHidden"/>
    
    点赞 4 评论 复制链接分享
  • csdnceshi78 程序go 6年前

    You can achieve this by creating a dummy EditText with layout width and height set to 0dp, and request focus to that view. Add the following code snippet in your xml layout:

    <EditText
        android:id="@+id/editText0"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:hint="@string/dummy"
        android:ems="10" 
        >
         <requestFocus />
        </EditText>
    
    点赞 4 评论 复制链接分享
  • csdnceshi80 胖鸭 7年前

    Simple solution: In AndroidManifest in Activity tag use

    android:windowSoftInputMode="stateAlwaysHidden"
    
    点赞 3 评论 复制链接分享
  • csdnceshi75 衫裤跑路 4年前

    Write this line in your Parent Layout...

     android:focusableInTouchMode="true"
    
    点赞 3 评论 复制链接分享
  • csdnceshi55 ~Onlooker 6年前

    This is the perfect and most easiest solution.I always use this in my app.

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    
    点赞 2 评论 复制链接分享
  • csdnceshi61 derek5. 10年前

    using the information provided by other posters, I used the following solution:

    in the layout XML

    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:id="@+id/linearLayout_focus"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="0px"
        android:layout_height="0px"/>
    
    <!-- AUTOCOMPLETE -->
    <AutoCompleteTextView
        android:id="@+id/autocomplete"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:inputType="textNoSuggestions|textVisiblePassword"/>
    

    in onCreate()

    private AutoCompleteTextView mAutoCompleteTextView;
    private LinearLayout mLinearLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
    
        //get references to UI components
        mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
        mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
    }
    

    and finally, in onResume()

    @Override
    protected void onResume() {
        super.onResume();
    
        //do not give the editbox focus automatically when activity starts
        mAutoCompleteTextView.clearFocus();
        mLinearLayout.requestFocus();
    }
    
    点赞 2 评论 复制链接分享
  • csdnceshi65 larry*wei 7年前

    For me, what worked on all devices is this:

        <!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
    
        <View
        android:layout_width="0px"
        android:layout_height="0px"
        android:focusable="true"
        android:focusableInTouchMode="true" />
    

    Just put this as a view before the problematic focused view, and that's it.

    点赞 2 评论 复制链接分享
  • csdnceshi68 local-host 10年前

    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>
    
    点赞 2 评论 复制链接分享
  • weixin_41568174 from.. 3年前

    Simplest answer, just add this in parent layout of the XML.

    android:focusable="true" 
    android:focusableInTouchMode="true"
    
    点赞 2 评论 复制链接分享

相关推荐