2013-11-21 214 views
0

我工作的一個女人懷孕的應用程序。 我正試圖讓寶寶踢上櫃臺。 點擊按鈕後,計數器增加。 但我希望每次點擊,按鈕顏色都像網頁懸停一樣。 我嘗試的onclick但一次改變結腸後來沒有改變如何改變我的顏色按鈕顏色

此示例代碼點擊數

@Override 
    public void onClick(View v) { 
     if(v == counterbutton) { 
      counterbutton.setBackgroundColor(getResources().getColor(R.color.blue_background)); 
      get_last_counter++; 
      SharedPreferences.Editor editor = MotherActivity.preferences.edit(); 
      editor.putInt(MotherActivity.COUNTER_INCREASE, get_last_counter); 
      editor.apply(); 
      counter.setText(counter_writer(get_last_counter)); 
     } 
    } 

按鈕的XML代碼

<Button 
        android:layout_width="250dp" 
        android:layout_height="wrap_content" 
        android:text="@string/kick" 
        android:id="@+id/counterbutton" 
        android:background="@color/app_pink" 
        android:textColor="@color/blue_text_color" 
        android:layout_centerVertical="true" 
        android:layout_alignParentLeft="true" /> 

對不起不好英語。 謝謝。

回答

2

我會建議尋找到選擇,here是一個很好的例子,我發現。

+0

是爲真正的答案感謝名單:) –

0

的每次點擊設置相同的顏色。你應該有像變量和備用:

mClickIndex = 0 
if(mClickIndex % 2 == 0){ 
counterbutton.setBackgroundColor(getResources().getColor(R.color.blue_background)); 
} else { 
counterbutton.setBackgroundColor(getResources().getColor(OTHER COLOR)); 
} 
mClickIndex++; 
+0

我想點擊改變顏色。像網絡懸停。 –

0
@Override 
    public void onClick(View v) { 
     int id = v.getId(); 
     switch(id) { 
     case R.id.counterbuton:   
       counterbutton.setBackgroundColor(getResources().getColor(R.color.blue_background)); 
       get_last_counter++; 
       SharedPreferences.Editor editor = MotherActivity.preferences.edit(); 
       editor.putInt(MotherActivity.COUNTER_INCREASE, get_last_counter); 
       editor.apply(); 
       counter.setText(counter_writer(get_last_counter)); 
     break; 
      } 
     } 
0

這會起作用。

Random rnd = new Random(); 
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
counterbutton.setBackgroundColor(color);