2017-09-26 239 views
1

我試圖在單擊時更改Button的背景顏色,當我釋放Button時按下Button時,我可以更改背景顏色該按鈕返回到默認值顏色。我不知道該從哪裏出發,我一直在試圖找到解決這個問題的辦法。Android單擊後更改背景顏色

`

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.game.dice.MainActivity"> 

    <Button 
     android:id="@+id/but1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 1" 
     android:background="@drawable/button_color" 
     /> 
    <Button 
     android:id="@+id/but2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 2" 
     android:background="@drawable/button_color"/> 
</LinearLayout> 

`

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
    android:drawable="@color/button_pressed"/> 
<item android:state_focused="true" 
    android:drawable="@color/button_focused"/> 
<item android:drawable="@color/button_default"/> 
</selector> 
+1

是否必須通過XML?否則,您可以使用按鈕OnClickListener。 –

回答

0

這是因爲默認情況下,當該按鈕被觸摸它將採取點擊而不是焦點。如果你想要按鈕被聚焦並改變它的顏色,當按下這個添加到y我們的按鈕在xml中。

android:focusableInTouchMode="true" 
0

在你的繪製,最後<item>表示默認狀態。改變這一點。

編輯:

對不起,我不好錯。無論您使用什麼,您都必須在Java或Kotlin中執行此操作。這是更多的業務邏輯,而不是「觀點」。

0

嘗試這樣的:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="true" android:state_pressed="false" android:color="@color/button_pressed" /> 
<item android:state_focused="true" android:state_pressed="true" android:color="@color/button_pressed" /> 
<item android:state_focused="false" android:state_pressed="true" android:color="@color/button_default" /> 
<item android:color="@color/button_pressed" /> 

0

試試這個


<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<!-- Non focused states --> 
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/on_the_go" 
    android:color="@color/colorText" /> 
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/on_the_go_selected" 
    android:color="@color/colorSkyBlue"/> 

<!-- Focused states --> 
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/on_the_go" 
    android:color="@color/colorText"/> 
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/on_the_go_selected" 
    android:color="@color/colorSkyBlue"/> 

<!-- Pressed --> 
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/on_the_go" 
    android:color="@color/colorText"/> 
<item android:state_pressed="true" android:drawable="@drawable/on_the_go_selected" 
    android:color="@color/colorSkyBlue" /> 

</selector> 

竭誠爲您服務

2

這是在Java中如何做到這一點的快速設置;

private Button mButton; 
mButton = (Button) findViewById(R.id.but1); 

mButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      mButton.setBackgroundColor(Color.BLACK); 
     } 
    }); 

你基本上做了一個ClickListener,只要單擊按鈕就會做出反應。從這裏我們將按鈕背景顏色更改爲黑色。如果您希望每次點擊都隨時更改,則可以應用更多邏輯。

+0

我喜歡試圖使用Java的想法, – morefaster

+0

我喜歡試圖使用Java的想法,我是新來的Android,我敢肯定我在這裏失去了一些東西,但我不知道是什麼,onClick看起來像這樣** onClick =「mButton」; **。我是否應該編輯問題中的代碼以反映我的更新代碼,或者在這裏認爲是不好的形式? – morefaster

+0

我給你的代碼就是你需要的一切,下面的文字只是對代碼的描述。我認爲這是最簡單的,如果你在你的文章底部添加一個**編輯:**,並把你的新代碼放在那裏。 –

0

你試過這個嗎?

myButton.setBackgroundColor(Color.RED); 

此代碼位於您的onClick方法中。

0

,如果你wan't重置按鈕的背景顏色爲默認不是使用下面的代碼試試這個

button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       button.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.colorBlack)); 
      } 
     }); 

注意

button.setBackgroundResource(android.R.drawable.btn_default); 
0

正確的XML是:

`<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
android:drawable="@color/button_pressed"/> 
<item android:state_focused="false" 
android:drawable="@color/button_focused"/> 
<item android:drawable="@color/button_default"/> 
</selector>` 

你不能集中所有的事實。