gpt4 book ai didi

java - setOnFocusChangeListener 改变背景颜色

转载 作者:行者123 更新时间:2023-11-29 21:14:18 27 4
gpt4 key购买 nike

我试图做到这一点,当用户关注 TextView 时,背景会改变颜色,然后当它失去焦点时,textview 会失去焦点颜色。你能帮忙吗?

TextView tv1=new TextView(this);

tv1.setOnFocusChangeListener(new OnFocusChangeListener() {

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

if(hasFocus){
v.setBackgroundColor(Color.RED);
}else{
v.setBackgroundColor(Color.TRANSPARENT);
}

}

});

最佳答案

默认textView是不可点击和不可聚焦的,那么你需要设置它。

试试这个。

tv1.setClickable(true);
tv1.setFocusableInTouchMode(true);

关于java - setOnFocusChangeListener 改变背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21848475/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com