Einstellung Hintergrund Drawable mit Staaten, android-Tasten

Baue ich eine multiple-choice-Fragebogen android-Programm.
Ich habe Folgendes in meiner onCreate () - Methode

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

Die drawable Ressource "optionbutton.xml" legt einen roten Farbverlauf für den "gedrückt-Zustand", und ein grauen für andere Staaten. Es sieht wie folgt

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

Den Selektor funktioniert tatsächlich, aber auf dem letzten Anwendung Taste nur. Egal, welche Schaltfläche vom 5 gedrückt wird, wird die Letzte (nicht gedrückt) - Taste wird nur der hintergrund rot.

Als debugging-Schritt: ich versuchte, Sie zu entrollen Sie die for-Schleife und ändern der Reihenfolge der Anwendung der hintergrund der letzten Anwendung Taste bekommt nur rot hintergrund:

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

Dies ist verwirrend!
Was ist falsch mit meiner Umsetzung?

Haben Sie versucht, diese (Taste)btnArray[0]).setBackgroundDrawable(Form);
ja habe ich versucht, gleiche problem. Ich werde Bearbeiten Sie die Frage, um Verwechslungen zu beseitigen.
wie über die Einstellung der hintergrund in XML?
auch Sie haben xmlns:android="http://schemas.android.com/apk/res/android" zweimal. nicht sicher, ob das ein problem sein.
Ich habe mehrere XML-Dateien, die auf run-time-ich wähle rot-hintergrund für falsche Antworten, grün-hintergrund für korrigieren.

InformationsquelleAutor Tarek Eldeeb | 2013-03-08

Schreibe einen Kommentar