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

后端 未结 30 2754
别那么骄傲
别那么骄傲 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:05

    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.

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

    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>
    
    0 讨论(0)
  • 2020-11-21 07:08

    Write this line in your Parent Layout...

     android:focusableInTouchMode="true"
    
    0 讨论(0)
  • 2020-11-21 07:08

    Easiest way to hide keyboard is using setSoftInputMode

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

    or you can use InputMethodManager and hide keyboard like this.

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    
    0 讨论(0)
  • 2020-11-21 07:10

    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;
    }
    
    0 讨论(0)
  • 2020-11-21 07: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).

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