以编程方式单击时突出显示 TextView [英] Highlight TextView when clicked programmatically
问题描述
我动态生成像按钮一样工作的 TextViews.现在我想在它们被按下时突出显示它们.诸如更改文本颜色或背景颜色之类的东西.我试过使用选择器,但它不起作用.
I dynamically generate TextViews which work like buttons. Now i want to highlight them when they get pressed. Something like change Text color or background color. I have tried to use selector, but it doesn't work.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ffffff"/>
<item android:state_pressed="true" android:state_enabled="false" android:color="#ffffff" />
</selector>
这里是我创建 TextViews 的循环.
Here my loop for creating the TextViews.
int z = 0;
for (MOKGenericDataItem d : data) {
if (d.getButtonText() != null) {
final int pagePosition = z;
TextView btn = new TextView(getActivity());
btn.setId(z);
final int id_ = btn.getId();
btn.setText(d.getButtonText());
btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
btn.setGravity(Gravity.CENTER);
mLineareLayoutViewPagerButtons.addView(btn);
btn1 = ((TextView) view.findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mViewPager.setCurrentItem(pagePosition,false);
}
});
}
z++;
}
推荐答案
首先,您的这一行会产生歧义,因为您将变量名称设为 btn1(将其与按钮相关联)并且您正在参考TextView
,
First of all your this line is creating ambiguity as you are taking a variable name as btn1 (which is relating it to button)and you are taking a reference of TextView
,
btn1 = ((TextView) view.findViewById(id_));
无论如何,一步一步来,
在
drawable
文件夹中创建一个类似于label_bg.xml
的 xml,如下所示:
create an xml like
label_bg.xml
like the following in thedrawable
folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressed_color"
android:state_pressed="true" />
<item android:drawable="@drawable/normal_color" />
</selector>
在 values
文件夹中创建另一个 xml
如下所示,命名为 labelcolors.xml
In values
folder create another xml
like the following,named as labelcolors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="pressed_color">#7ec0ee</drawable> <!--custom color for pressed state -->
<drawable name="normal_color">#00FFFFFF</drawable> <!--transperent color for normal state -->
</resources>
现在设置标签的背景为label_bg.xml
<TextView
android:id="@+id/yourlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="760dp"
android:layout_marginTop="515dp"
android:background="@drawable/label_bg" <!--like this-->
android:text="LabelText"
android:textSize="20dp" />
当您动态添加视图时,您需要以编程方式为每个 textView 设置背景.对于 textview<上的调用
setBackgroundResource()
/code> 对象创建并设置 label.xml
作为背景
as you are dynamically adding the views so you need to set the background for your each textView programatically .For that call setBackgroundResource()
on the textview
object created and set label.xml
as background
这篇关于以编程方式单击时突出显示 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!