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

后端 未结 30 2698
别那么骄傲
别那么骄傲 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.

    
    

    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.

提交回复
热议问题