2012-06-08 54 views
1

的第一篇文章請溫柔...的Android動態的onClick

此代碼創建一個表10×10全在textviews simples連字符...

隨着onClickListner我需要它來改變顏色,如果一文點擊。

失敗的tv.setTextColor(Color.RED);

可以進行排序,或是否有更好的辦法?

謝謝。

_temp類:

public class _Temp extends Activity implements OnClickListener { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.a_temp); 


    TableLayout treeTable = (TableLayout) findViewById(R.id.main_table); 

    // Add 10 Rows/10 Cols ~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    int idxCount = 0; 

    for (int tree = 1; tree <= 10; tree++) { 
     idxCount = 0; 

     TableRow TableRows = new TableRow(this); 
     TableRows.setId(tree * 100 + idxCount); 
     TableRows.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     TableRows.setBackgroundColor(Color.LTGRAY); 
     idxCount++; 

     for (int bird = 1; bird <= 10; bird++) { 
      TextView label_TableCols2 = new TextView(this); 
      label_TableCols2.setId(tree * 100 + idxCount); 
      label_TableCols2.setBackgroundColor(Color.DKGRAY); 
      label_TableCols2.setClickable(true); 
      label_TableCols2.setOnClickListener(this); 

      label_TableCols2.setText("-"); 

      TableRows.addView(label_TableCols2); // add the column to the table row 
      idxCount++; 
     } 
     treeTable.addView(TableRows, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    } 
} 

public void onClick(View v) { 
    Log.i("scoreTable", "Something Clicked - ID = " + Integer.toString(v.getId())); 

    int resId = getResources().getIdentifier(Integer.toString(v.getId()), "id", getPackageName()); 

    Log.i("scoreTable", "Colour Update 1 - resId = " + Integer.toString(resId)); 

    TextView tv = (TextView) findViewById(resId); 

    Log.i("scoreTable", "Colour Update 2 - Just to see if it gets this far"); 

    tv.setTextColor(Color.RED); 

    Log.i("scoreTable", "Colour Update 3 - Just to see if it gets this far"); 
} 
} 

和a_temp.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <HorizontalScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

       <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <TableLayout 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/main_table" 
       android:layout_width="match_parent" 
       android:layout_height="fill_parent" > 

      </TableLayout> 

     </LinearLayout> 

    </HorizontalScrollView> 

</ScrollView> 

編輯: OK,這是太容易了...怎麼樣從充氣!

,單擊某個項目表中的充氣彈出,挑選顏色,然後單擊「選擇顏色」按鈕,連字符的更新顏色點擊。

public void onClick(View v) { 
    ((TextView) v).setTextColor(Color.RED); // <--- SWEET, WORKING 

    AlertDialog.Builder PopUpBuilder = new AlertDialog.Builder(this); 

    //... 

    PopUpBuilder.setPositiveButton("Choose Colour", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 

      // Do stuff and store global variables 

      updateTable(); 
     } 
    }); 
    AlertDialog PopupDialog = PopUpBuilder.create(); 
    PopupDialog.show(); 
} 

public void updateTable() { 

    TextView scoreTableTitle = (TextView) findViewById(R.id.textTitle1); 
    scoreTableTitle.setText("woo woo"); // <--- WORKING 

    ((TextView) v).setTextColor(Color.BLUE); // <--- ????   
} 

回答

3

的點擊View是TextView的,所以你只需要設定自己的顏色在你的聽衆:

public void onClick(View v) { 
    ((TextView) v).setTextColor(Color.RED); 
} 
+0

完美,快速,WOW – Sparki