0

我有一個表格佈局,每行都有兩個textview。我想在選中該行時更改textview的文本顏色。我也使用文本視圖的文本顏色選擇器xml,但選擇該行時顏色不變。 這裏是XML當在表格佈局中選擇行時,更改TextView的文本顏色Android

<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

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

把你的代碼也放在這裏也http://www.compiletimeerror.com/2014/03/android-button-selector-tutorial-with.html – 2015-02-11 05:36:59

回答

0

在項目中添加Text_selector XML RES /彩色目錄下然後從TextView中引用作爲

機器人:文字顏色=「@顏色/ 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; 
      } 
     }); 

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

+0

當文本被選中時顏色發生變化,但是我想改變文本選擇行時的顏色就像列表視圖一樣。當你選擇列表時,當列表被突出顯示時,文本顏色被改變。 – user3835770 2015-02-11 06:34:38

+0

你是否嘗試過** setOnTouchListener **表格行?在ontouch事件中,獲取textview並更改其顏色。 – Madhukar 2015-02-11 06:43:47

+0

PLZ給我舉一些例子來解決這個問題 – user3835770 2015-02-11 06:52:45

相關問題