2013-12-23 2 views
0
나는 그것의 바닥에 몇 가지 버튼이 있습니다 안드로이드 활동,이 버튼은 하나를 눌렀습니다

및 기본값으로 발표 한 세버튼 이상한 행동

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 모양 :

01,

이 내 코드입니다

<?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를 수행하면 활동 이름이