2014-09-15 3 views
0

클릭하면 내 버튼의 색상 (텍스트 & 배경)을 반전하고 싶습니다.Android 변경 버튼 배경

다음 XML 파일이 있습니다. 테스트 할 때 텍스트 색 변경을 볼 수 있지만 배경색은 그대로 유지됩니다. 내가 빠진 것을 볼 수 없었다.

layout.xml

... 
<Button 
    android:id="@+id/start_btn" 
    android:layout_gravity="center_horizontal|center_vertical" 
    android:layout_margin="0dp" 
    android:text="start" 
    android:background="@drawable/button_background" 
    android:textColor="@drawable/button_text_color" 
    android:fontFamily="@string/font_family" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    /> 
... 

button_text_color.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:color="@color/miops_dark_red" /> 
    <item android:color="@android:color/white" /> 
</selector> 

button_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:state_focused="true"> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
      <solid android:color="@android:color/white" /> 
      <stroke 
       android:width="1dp" 
       android:color="@color/miops_dark_red" /> 
      <padding 
       android:bottom="1dp" 
       android:left="1dp" 
       android:right="1dp" 
       android:top="1dp" /> 
     </shape> 
    </item> 
    <item> 
     <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
      <solid android:color="@color/miops_dark_red" /> 
      <stroke 
       android:width="1dp" 
       android:color="@android:color/white" /> 
      <padding 
       android:bottom="1dp" 
       android:left="1dp" 
       android:right="1dp" 
       android:top="1dp" /> 
     </shape> 
    </item> 
</selector> 

답변

1

마침내 나는이 문제를 해결할 수있었습니다. 다음과 같이 background.xml을 수정했습니다.

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

및 추가 2 개;

button_background_pressed.xml

<?xml version="1.0" encoding="utf-8"?> 

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="@android:color/white" /> 
<stroke 
    android:width="1dp" 
    android:color="@color/miops_dark_red" /> 
<padding 
    android:bottom="1dp" 
    android:left="1dp" 
    android:right="1dp" 
    android:top="1dp" /> 
</shape> 

button_background_released.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@color/miops_dark_red" /> 
    <stroke 
     android:width="1dp" 
     android:color="@android:color/white" /> 
    <padding 
     android:bottom="1dp" 
     android:left="1dp" 
     android:right="1dp" 
     android:top="1dp" /> 
</shape> 
0
mLogin.setOnTouchListener(new View.OnTouchListener() { 
@Override 
public boolean onTouch(View v, MotionEvent event) { 
if(event.getAction() == MotionEvent.ACTION_DOWN) { 
mLogin.setBackgroundColor(Color.rgb(AESConstants.RED_DOWN,AESConstants.GREEN_DOWN,AESConstants.BLUE_DOWN)); 
} 
else if(event.getAction() == MotionEvent.ACTION_UP) { 
mLogin.setBackgroundColor(Color.rgb(AESConstants.RED_UP,AESConstants.GREEN_UP,AESConstants.BLUE_UP)); 
} 
return false; 
} 
}); 

이 하나의 방법 당신이 뭔가를 할 수있다 레이아웃 파일 대신 코드 자체에서 코드

0

아래 코드에서 btn_bak.xml을 변경 (또는 문제가 있다고 생각되는 그라디언트를 추가)하면 테두리 (획) 만 사용합니다. 아래 내 응용 프로그램에서 사용하고 올바른 질문을 이해하면 귀하의 요구에 쉽게 수정할 수 있습니다. 단추를 누를 때 단추에 노란색 배경이 나타나고 검정색은 단추 주위의 프레임에 불과하며 아무것도없는 경우 흰색 배경이됩니다. 작동하는지 알려주세요.

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 

<item android:state_pressed="true"> 
    <shape> 
     <gradient 
      android:startColor="@color/yellow1" 
      android:endColor="@color/yellow" 
      android:angle="270" /> 
     <stroke 
      android:width="3dp" 
      android:color="@color/black" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:top="20dp" 
      android:bottom="10dp" /> 
    </shape> 
</item> 

<item>   
    <shape> 
     <gradient 
      android:endColor="@color/white" 
      android:startColor="@color/white" 
      android:angle="270" /> 
     <padding 
      android:top="20dp" 
      android:bottom="10dp" /> 
    </shape> 
</item> 
</selector> 
0

이 시도 ..

@SuppressLint ("ResourceAsColor") 개인 무효 setBackgroundColor (문자열 색상, 버튼 BTN) {

GradientDrawable drawable = (GradientDrawable)btn.getBackground(); 
    drawable.setColor(Color.parseColor(color)); 
    drawable.setStroke(4,Color.parseColor(color)); 
    btn.setColorFilter(Color.parseColor("#ffffff")); 
}