Setting Background Drawable with states to android buttons

╄→尐↘猪︶ㄣ 提交于 2019-12-07 11:52:52

问题


I am building a multiple choice questionnaire android program. I have the following in my onCreate() method

btnArray = new Button[5];
btnArray[0] = (Button) findViewById(R.id.bOp1);
btnArray[1] = (Button) findViewById(R.id.bOp2);
btnArray[2] = (Button) findViewById(R.id.bOp3);
btnArray[3] = (Button) findViewById(R.id.bOp4);
btnArray[4] = (Button) findViewById(R.id.bOp5);

Typeface othmanyFont = Typeface.createFromAsset(getAssets(),
        "fonts/amiri.ttf");
Drawable shape = getResources().getDrawable(R.drawable.optionbutton);

for(int i=0;i<5;i++){
    btnArray[i].setTypeface(myFont);
    ((Button)btnArray[i]).setBackgroundDrawable(shape); //Button-4 only turns red
    btnArray[i].setOnClickListener(this);
}  

The drawable resource "optionbutton.xml" defines a red colour gradient for the "pressed state", and a grey for other states. It looks as follows

<?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="#bf1d00"
      android:endColor="#801300"
      android:angle="270" />
        <corners android:radius="10dp" />
    <stroke
      android:width="1dp"
      android:color="#71c2eb" />
    </shape>
  </item>
  <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient android:startColor="#FFFFFF" 
                android:endColor="#999"
                android:angle="270" />
            <corners android:radius="10dp" />
            <stroke android:width="1px" android:color="#0070b7" />
        </shape>
  </item>
</selector>

The selector actually works, but on the last applied button only. No matter which button from the 5 is pressed, the last (not the one pressed) button only gets its background red.

As a debugging step: I tried to unroll the for-loop and change order of applying the background, the last applied button only gets red background:

((Button)btnArray[0]).setBackgroundDrawable(shape);
((Button)btnArray[1]).setBackgroundDrawable(shape);
((Button)btnArray[2]).setBackgroundDrawable(shape);
((Button)btnArray[4]).setBackgroundDrawable(shape);
((Button)btnArray[3]).setBackgroundDrawable(shape); //Button-3 only turns red

This is confusing! What's wrong with my implementation?


回答1:


It seems setBackgroundDrawable() is deprecated. You can use another alternative like setBackgroundResource().




回答2:


you can set shape in one xml and button state in another then try here is xml for button to get different state

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

    <item android:drawable="@drawable/save_button_active" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="true"/>
    <item android:drawable="@drawable/save_button_inactive" android:state_focused="false" android:state_pressed="false"/>
    </selector>



回答3:


As the method setBackgroundDrawable(shape) is deprecated, I tried another method that actually works perfect and solved the problem:

((Button)btnArray[i]).setBackgroundResource(R.drawable.optionbutton);


来源:https://stackoverflow.com/questions/15287083/setting-background-drawable-with-states-to-android-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!