선다형 설문 조사 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"은 "을 누르면 상태"에 대한 붉은 색 그라데이션을 정의하고, 다른 국가에 대한 회색으로 다음 있습니다. 다음과 같이 표시됩니다.
<?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>
선택기는 실제로 작동하지만 마지막으로 적용된 버튼에서만 작동합니다. 5의 어떤 버튼이 눌러 졌는지에 관계없이 마지막 버튼 (눌러 진 것이 아닌) 버튼은 배경색 만 빨강으로 표시합니다. 디버깅 단계로
: 나는 대한 루프를 풀다과 배경을 적용하는 순서를 변경하는 시도는, 마지막으로 적용된 버튼은 빨간색 배경을 가져옵니다
((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
이 혼란입니다! 구현에있어 문제가 있습니까?
시도해 보셨습니까 ((Button) btnArray [0]). setBackgroundDrawable (shape); – DjHacktorReborn
예 동일한 문제가 발생했습니다. 혼란을 없애기 위해 질문을 편집하겠습니다. –
XML의 배경 설정은 어떻습니까? – paul