android 绘制按钮,将背景可绘制的状态设置为android按钮

我正在建立一个选择题问卷的android程序.

我的onCreate()方法中有以下内容

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);

}

可绘制资源“ optionbutton.xml”为“按下状态”定义了红色渐变,为其他状态定义了灰色.看起来如下

android:startColor="#bf1d00"

android:endColor="#801300"

android:angle="270" />

android:width="1dp"

android:color="#71c2eb" />

android:shape="rectangle">

android:endColor="#999"

android:angle="270" />

选择器实际上有效,但仅在最后应用的按钮上有效.无论按下5中的哪个按钮,最后一个按钮(而不是按下的按钮)只会变成红色背景.

作为调试步骤:我尝试展开for循环并更改应用背景的顺序,最后应用的按钮仅获得红色背景:

((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

这很混乱!

我的实现有什么问题?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值