2014-05-16 107 views
1

所以我試圖讓這個EditText從textColor轉到另一個當我按下特定按鈕時(例如:button1將textcolor更改爲BLUE,button2更改它GREEN等)用按鈕單擊更改TextView的值(顏色)

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/test" 
    android:id="@+id/test" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:textColor="@color/orange_test" 
    android:layout_toLeftOf="@+id/colorBtn" 
    android:layout_alignBottom="@+id/colorBtn" 
    android:textSize="25sp" 
    android:gravity="center_vertical|center_horizontal" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/colorBtn" 
    android:id="@+id/colorBtn" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:onClick="changeColor"/> 

我該如何繼續? :\

+0

看看' OnClickListeners' –

回答

1

複製您的按鈕來設置一個以上的顏色

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/colorBtn" 
    android:id="@+id/colorBtn" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:onClick="changeColor" 
/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/colorBtnRed" 
    android:id="@+id/colorBtnRed" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:onClick="changeColor" 
/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/colorBtnGreen" 
    android:id="@+id/colorBtnGreen" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:onClick="changeColor" 
/> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/colorBtnBlue" 
    android:id="@+id/colorBtnBlue" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:onClick="changeColor" 
/> 

在你changeColor,檢查一下按鍵的ID。如果它是colorButtonBlue ID,則將EditText的文本顏色設置爲藍色。如果是colorButtonRed,療法設置爲紅色,ABD等...

public void changeColor(View v) 
{ 
    switch(v.getId()) 
    { 
     case R.id.colorBtnRed: 
      edtTest.setTextColor(Color.RED); 
      break; 
     case R.id.colorBtnGreen: 
      edtTest.setTextColor(Color.GREEN); 
      break; 
     case R.id.colorBtnBlue: 
      edtTest.setTextColor(Color.BLUE); 
      break; 
     default: 
      edtTest.setTextColor(Color.BLACK); 
    } 
} 

[編輯]

當然,在你的聲明部分,這樣做:

EditText edtTest = null; 

而且在你的onCreate做這樣的事情(在super和setContentVieew行之後):

edtTest = (EditText) findViewById(R.id.test); 
+0

好吧,也許我明白了!最後一件事:我該怎麼處理editText1?它應該是我的textview,所以我怎麼可以鏈接這些? – Lampione

+0

您將一個EditText命名爲一個EditText(並且在佈局中顯示了一個EditText),而不是一個TextView:'所以我試圖讓這個EditText從一個textColor轉到另一個'...但是您可以輕鬆地從editText1更改爲textView1在顯示的代碼中。 –

+0

我編輯了我的答案,如有疑問 –

0

你的意思是這樣的? -

Button greenBtn = (Button) findViewById(R.id.green_button); 
button1.setOnClickListener(new OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          editText1.setTextColor(Color.GREEN); 
         } 
        });); 
+0

對不起,我很新:S 我必須導入button1嗎?或者我只需要用我的按鈕ID替換它? editText1是同樣的東西嗎?謝謝! :)) – Lampione

+0

你必須用你的項目名稱替換button1和editText1 ......但首先你必須使用如下代碼來分配對視圖的引用:Button greenBtn =(Button)findViewById(R.id.green_button) ; – iDemigod