OnFocusChangeListener doesn't work on Textview in android

0

There are two textview: textview1 and textview2, and their background color are white initially.I want to implement such an effect:

Click textview1—— textview1 turns black and textview2 turns white;

Click textview2—— textview1 turns white and textview1 turns black;

Obviously, I need to set OnFocusChangeListener on them, and set their XML attribute "focusable=true", as answered by many people in stackoverflow.

  <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintBottom_toBottomOf="parent"
       android:id="@+id/text1"
       android:textSize="50dp"
       android:focusable="true"
       android:text="xxxxxxxxxxx"

       />

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="50dp"
       android:text="yyyyyyyyyyyy"
       android:id="@+id/text2"
       android:focusable="true"

       app:layout_constraintBottom_toTopOf="@+id/text1"
       app:layout_constraintRight_toRightOf="parent"
       />

public class MainActivity extends AppCompatActivity {

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

        TextView textView1=findViewById(R.id.text1);
        textView1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    v.setBackgroundColor(0xffffffff);
                }else{
                    v.setBackgroundColor(0xff000000);
                }
            }
        });
        TextView textView2=findViewById(R.id.text2);
        textView2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    v.setBackgroundColor(0xffffffff);
                }else{
                    v.setBackgroundColor(0xff000000);
                }
            }
        });

//        textView1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                v.requestFocus();
//            }
//        });
//        textView2.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                v.requestFocus();
//            }
//        });
    }
}

But they don't work.I enter the debug mode, and find the truth that the "onFocusChange" never be triggered.

The only way they work, is to set their XML attributes android:focusableInTouchMode="true",but that's not I want, because I have to click a textview twice to trigger an OnClick event after setting this attribute. Besides,I believe it's not an elegant way to do this——I need to know the reason why setOnFocusChangeListener doesn't work.

android
focus
listener
asked on Stack Overflow Jun 12, 2020 by vainquit • edited Jun 13, 2020 by vainquit

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0