以编程方式单击时突出显示TextView

Dan*_*rch 4 android textview

我动态生成像按钮一样工作的TextViews.现在我想在他们受到压力时强调它们.像改变文本颜色或背景颜色的东西.我试图使用选择器,但它不起作用.

<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>
Run Code Online (Sandbox Code Playgroud)

这是我创建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++;
    }
Run Code Online (Sandbox Code Playgroud)

Dil*_*lip 7

您需要创建一个类implements withOnTouchListener和Detect touch MotinACTION_DOWNACTION_UP根据您的要求更改文本颜色并更改其默认颜色。

代码:

public class CustomTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            ((TextView) view).setTextColor(0xFFFFFFFF); // white
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            ((TextView) view).setTextColor(Color.parseColor("#4a4a4a")); // lightblack
            break;
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在使用设置 TouchListener

textView.setOnTouchListener(new CustomTouchListener());
Run Code Online (Sandbox Code Playgroud)


nob*_*alG 5

首先,你的这一行会产生歧义,因为你将变量名称作为btn1(将其与按钮相关联)并且你正在参考TextView,

 btn1 = ((TextView) view.findViewById(id_));
Run Code Online (Sandbox Code Playgroud)

无论如何,一步一步走,

  • 创建一个类似于label_bg.xml以下drawable文件夹中的xml :

     <?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>
    
    Run Code Online (Sandbox Code Playgroud)
  • values文件夹中创建另一个xml,如下所示,命名为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>
    
    Run Code Online (Sandbox Code Playgroud)
  • 现在将标签的背景设置为 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" />
    
    Run Code Online (Sandbox Code Playgroud)

当您动态添加视图时,您需要以编程方式为每个textView设置背景.对于创建setBackgroundResource()textview对象的调用并设置label.xml为背景