简体   繁体   English

在表格布局Android中选择行时,更改TextView的文本颜色

[英]change the Text color of TextView when the row is selected in Table Layout Android

I have a table layout and every row i have a two textview. 我有一个表格布局,每一行都有两个textview。 i want to change the text color of textview when the row is selected. 我想在选择行时更改textview的文本颜色。 I Also use the selector xml in text color of textview but the color is not changing when the row is selected. 我还在textview的文本颜色中使用选择器xml,但选择行时颜色不会改变。 Here is the xml 这是XML

 <TableRow  

        android:id="@+id/row1"
        android:onClick="rowClick"
        android:focusable="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:background="@drawable/selector"  //this selector is use for row when its selected  

        >

       <LinearLayout
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:background="@drawable/textline"
           android:orientation="horizontal" >

           <TextView
               android:id="@+id/username1" 
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="User Name "
               android:textColor="@drawable/text_selector"       
               android:gravity="left|center"
               android:textSize="16dip"

                />

           <TextView
               android:id="@+id/userName"
               android:layout_width="86dp"
               android:layout_height="match_parent"
               android:layout_weight="0.27"
               android:gravity="right|center"
               android:freezesText="true"
               android:text=""
               android:textSize="16dip"
               android:textColor="#c4c0A3" />
       </LinearLayout>

        </TableRow>

Text_selector xml Text_selector xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
    <item android:state_focused="true" android:state_pressed="true" android:color="#ffffff" />
    <item android:state_focused="false" android:state_pressed="true" android:color="#ffffff" />
    <item android:color="#000000" />
</selector>

Here Is the code 这是代码

row1.setOnClickListener(new OnClickListener()
        {                                   
            public void onClick(View v) 
            {

final EditText m_objText  = (EditText) promptsView.findViewById(R.id.username_pref);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        alertDialogBuilder.setTitle(sTitle);
        alertDialogBuilder.setView(promptsView);

        // set dialog message
        alertDialogBuilder
                .setCancelable(false)
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() 
                {
                            public void onClick(DialogInterface dialog, int id) 
                            {
                                objRowTextView.setText(m_objText.getText());
                                HideKeyboard();

                            }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() 
                {
                            public void onClick(DialogInterface dialog, int id) 
                            {
                                HideKeyboard();
                                dialog.cancel();
                            }
                });

Add Text_selector xml in your project under res/color directory and then refer from the TextView as 在res / color目录下的项目中添加Text_selector xml,然后从TextView引用为

android:textColor="@color/Text_selector" 机器人:文字颜色= “@颜色/ Text_selector”

Hope you can do same in code by setting different color for different action. 希望您可以通过为不同的动作设置不同的颜色来在代码中执行相同的操作。

TextView text1 = (TextView) view.findViewById(R.id.textview);
text1.setOnTouchListener(new OnTouchListener() {
@Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                text1.setTextColor(Color.parseColor("#6DC066"));
                text1.setBackgroundColor(Color.parseColor("#FFFFFF"));
                    break;
                case MotionEvent.ACTION_UP:
                    v.performClick();
                    break;}
                return true;
            }
        });

let me know anything apart from this. 让我知道除此之外的任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM