2013-12-23 213 views
0

我已經在它的下方的一些按鈕一個Android活動,這些按鈕都一個一個按下並釋放爲默認三,按鈕按下

enter image description here

,並在任何時候奇怪的行爲他們按下它的背景應該改變到按下按鈕的背景和其他背景應該改變,以釋放按鈕的背景,但點擊任何按鈕時,我得到的結果是:

enter image description here

這是我的代碼:

的xml:

<LinearLayout 
      android:id="@+id/GrandThree" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_marginTop="10dp" 
      android:layout_gravity="center" 
      android:gravity="center" 
      android:orientation="horizontal" 
      android:background="@android:color/transparent"> 

      <Button 
       android:id="@+id/BedRoom_bottom" 
       android:layout_width="200dp" 
       android:layout_height="fill_parent" 
       android:background="@drawable/light_bottom_buttons_pressed" 
       android:text="Bedroom" 
       android:textSize="20dp" 
       android:textColor="#FFFFFF" 
       android:layout_gravity="center" 
       android:layout_marginTop="3dp" 
       android:layout_marginLeft="3dp" 
       android:layout_marginBottom="3dp" 
       android:layout_marginRight="3dp" 
       android:tag="pressed" 
       android:clickable="true" /> 

      <Button 
       android:id="@+id/livingroom" 
       android:layout_width="200dp" 
       android:layout_height="fill_parent" 
       android:background="@drawable/light_bottombuttons" 
       android:text="Living Room" 
       android:layout_gravity="center" 
       android:textSize="20dp" 
       android:padding="20px" 
       android:textColor="#FFFFFF" 
       android:layout_marginTop="3dp" 
       android:layout_marginLeft="3dp" 
       android:layout_marginBottom="3dp" 
       android:layout_marginRight="3dp" 
       android:tag="released" 
       android:clickable="true" /> 
      <!.. then two buttons the same way ..!> 
</LinearLayout> 

JAVA:

 // the onClickListener method for the Navigation buttons 
      public OnClickListener onNavigationClick = new OnClickListener() { 

      @Override 
       public void onClick(View v) { 


        if(v == bedRoom){ 

       String tag = bedRoom.getTag().toString().trim(); 
        if (tag.equals("released")) { 

         bedRoom.setBackgroundColor(R.drawable.light_bottom_buttons_pressed); 
         bedRoom.setTag("pressed"); 
         livingRoom.setBackgroundColor(R.drawable.light_bottombuttons); 
         livingRoom.setTag("released"); 
         masterBedroom.setBackgroundColor(R.drawable.light_bottombuttons); 
         masterBedroom.setTag("released"); 
         kitchen.setBackgroundColor(R.drawable.light_bottombuttons); 
         kitchen.setTag("released"); 

     } 
    } 
     //then another 3 blocks the same way for the other buttons 
} 
}; 

提示: light_bottombuttons & light_bottom_buttons_pressed是形狀,其具有漸變的顏色:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners android:radius="30dp"/> 

    <gradient 
     android:startColor="#353535" 
     android:centerColor="#212121" 
     android:endColor="#121212" <!.. the values of the other is just upside down these values ..!> 
     android:angle="270"/> 

    </shape> 

回答

1

創建一個文件中包含此您可繪製文件夾,名爲button-drawable.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:state_selected="true" 
    android:@drawable/light_bottom_buttons_pressed" /> 
<item 
    android:drawable="@drawable/light_bottom_buttons_pressed" /> 
</selector> 

爲佈局文件中的所有按鈕添加標籤。

android:background="@drawable/button_drawable" 

現在,在按鈕的點擊監聽器中,只要選擇了新按鈕,就將按鈕的選擇狀態設置爲true。

示例代碼:

public OnClickListener onNavigationClick = new OnClickListener() { 

     @Override 
      public void onClick(View v) { 


       if(v == bedRoom){ 

      String tag = bedRoom.getTag().toString().trim(); 
       if (tag.equals("released")) { 

        bedRoom.setTag("pressed"); 
        bedRoom.setSelected(true); 
        livingRoom.setTag("released"); 
        livingRoom.setSelected(false); 
        masterBedroom.setTag("released"); 
        masterBedroom.setSelected(false); 
        kitchen.setTag("released"); 
        kitchen.setSelected(false); 

    } 
} 
    //then another 3 blocks the same way for the other buttons 
} 
}; 
0

爲此使用無線電組和單選按鈕。 您的收音機組將包含多個按鈕,並且一次只能選擇一個。

0

當您在繪製你應該這樣做像下面

 bedRoom.setBackgroundColor(MyActivity.this.getResources().getDrawable(R.drawable.light_bottom_buttons_pressed)); 

MyActivity將改變是無論你的活動名稱是