2013-03-08 5 views
2

선다형 설문 조사 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 

이 혼란입니다! 구현에있어 문제가 있습니까?

+1

시도해 보셨습니까 ((Button) btnArray [0]). setBackgroundDrawable (shape); – DjHacktorReborn

+0

예 동일한 문제가 발생했습니다. 혼란을 없애기 위해 질문을 편집하겠습니다. –

+0

XML의 배경 설정은 어떻습니까? – paul

답변

1

setBackgroundDrawable()이되지 않습니다 것입니다. setBackgroundResource()과 같은 다른 대안을 사용할 수 있습니다.

3

다른이 여기에 시도 한 XML의 모양과 버튼의 상태를 설정할 수 있습니다이되지 않습니다 버튼을 다른 상태 메소드 setBackgroundDrawable (모양)으로

<?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> 
0

을 취득하는 XML, 나는 실제로 다른 방법을 시도 완벽하게 작동하고 문제 해결 :

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

도움을 주신 vmironov, DjHacktorReborn 및 paul에게 감사드립니다. –