Android:在关注父级时更改TextView textColor

时间:2009-10-17 13:08:57

标签: java android focus themes

TextView内有LinearLayoutLinearLayout能够获得焦点,我希望textColor的{​​{1}}能够得到关注。我认为使用ColorStateList会有效,但TextView似乎TextView没有得到焦点。我知道,因为我尝试过这段代码:

LinearLayout

没有任何记录。我不想在LinearLayout上使用mTextView.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Log.d(TAG, "Changed TextView focus to: " + hasFocus); } }); 来更改OnFocusChangeListener的{​​{1}},我认为这必须从XML完成。这样做的原因是,在另一项活动中,我有textColor个自定义适配器和自定义视图,Android会更改我TextView中的ExpandableListView s(从亮到暗)项目聚焦时的自定义视图。

2 个答案:

答案 0 :(得分:10)

这是一篇旧帖子,但由于我遇到了同样的问题,我发现这是XML属性:

android:duplicateParentState="true"

(在布局状态发生变化时添加到TextView以更改其“聚焦”状态)

答案 1 :(得分:4)

您可以在LinearLayout的侦听器的onFocuseChange方法中获取TextView。像

这样的东西
public void onFocusChange(View v, boolean hasFocus) {
    TextView tv = (TextView)v.findViewById(R.id.myTextView);
    tv.setTextColor(R.color.foo);
}

由于您的LL可以托管多个小部件,我认为即使您只有一个控件,LL的onFocus也不会传播