Is there a way to make EditText focusable with disabled focusableInTouchMode?

135 Views Asked by At

I have some EditTexts, and I want to control their focus order programmatically. However, EditText will get focus when touched by default, so this will break the order.

I have tried to make EditText's focusableInTouchMode false, but it never got focus then, like it has disabled focusable.

So, there is a way to disable it's focusableInTouchMode but still focusable?

1

There are 1 best solutions below

1
On

hope it's impossible Alan.Work around is ,Create one fake editText with 0dp hight and 0dp width.

      <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:fitsSystemWindows="true">
        <EditText
            android:id="@+id/edit_fake"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:focusable="true"
    
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            />
    
        <EditText
            android:id="@+id/edit_title"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
    
            android:layout_margin="32dp"/>
        <EditText
            android:id="@+id/edit_name"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/edit_title"
            app:layout_constraintStart_toStartOf="parent"
    
            android:layout_margin="32dp"/>
        <Button
            android:id="@+id/btn_done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="done"
            app:layout_constraintTop_toBottomOf="@id/edit_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

//activity code


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       fake=findViewById(R.id.edit_fake);
       name=findViewById(R.id.edit_name);
       title=findViewById(R.id.edit_title);
       done=findViewById(R.id.btn_done);

       fake.setOnFocusChangeListener(this);
       name.setOnFocusChangeListener(this);
       title.setOnFocusChangeListener(this);

       done.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btn_done){
            //clear focus
            name.clearFocus();
            fake.clearFocus();
            fake.requestFocus();

        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        if(v.getId()==R.id.edit_name){
            Log.d("onFocusChangename:", "name");
       }
        if(v.getId()==R.id.edit_fake){
            Log.d("onFocusChangenfake:", "fake");
        }

        if(v.getId()==R.id.edit_title){
            Log.d("onFocusChangenfake:", "tit");
        }
    }