【问题标题】:change the Text color of TextView when the row is selected in Table Layout Android在 Table Layout Android 中选择行时更改 TextView 的文本颜色
【发布时间】:2015-02-11 05:28:37
【问题描述】:

我有一个表格布局,每一行都有两个文本视图。我想在选择行时更改 textview 的文本颜色。我还在TextView的文本颜色中使用Selector 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>

文本选择器 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>

这是代码

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();
                            }
                });

【问题讨论】:

标签: android xml android-layout textview android-tablelayout


【解决方案1】:

在项目的 res/color 目录下添加 Text_selector xml,然后从 TextView 中引用为

android:textColor="@color/Text_selector"

希望您可以通过为不同的操作设置不同的颜色来在代码中做同样的事情。

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;
            }
        });

让我知道除此之外的任何事情。

【讨论】:

  • 选择文本时颜色会发生变化,但我想在选择行时更改文本颜色,就像列表视图一样。当您选择列表时,文本颜色会在列表突出显示时更改
  • 您是否尝试过 setOnTouchListener 用于表格行?在 ontouch 事件中获取 textview 并更改其颜色。
  • 我使用这个函数,但选择textview span>时颜色会更改
  • 是选择TextView时的颜色,但我想选择行不是TextView的行,只有 span>
猜你喜欢
  • 1970-01-01
  • 2012-04-16
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多