2012-06-15 22 views
5

Ben koşulu RadioButton"Radiobutton" işaretli mi kontrol edilir?</p> <pre><code>regAuxiliar = ultimoRegistro; </code></pre> <p>Ve Radiobuton:

Ben RadioButton RB1 seçildiğinde, bu fonksiyon aktif olduğunu istiyorsunuz (else-if) ile bir yapı yapmak istiyorum Ben Brezilyalı değilim, benim İngilizce için

regAuxiliar = objRegistro; 

Ve üzgün: RB2 seçilirse, bu fonksiyon aktiftir.

+2

(radyo düğmesi widget API belgelerine bakın http://developer.android.com/reference /android/widget/RadioButton.html) – Saurabh

cevap

0

radiobuttonObj.isChecked() Eğer Radion düğmesi yanlış aksi takdirde seçilirse

if(radiobuttonObj1.isChecked()){ 
//do what you want 
}else if(radiobuttonObj2.isChecked()){ 
//do what you want 
} 
+0

Çalışmıyor bile. – IMustBeSomeone

1

radioButton.isChecked() fonksiyonu true döndürür boolean verecektir.

Source

19

Sadece CheckBox

RadioButton rb; 

rb = (RadioButton) findViewById(R.id.rb); 

rb.isChecked(); 
+0

Döküm gerektirmez – IMustBeSomeone

6

yaptığınız gibi de dinleyici dayalı bir bayrak değerini koruyabilir,

radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 

       //handle the boolean flag here. 
        if(arg1==true) 
         //Do something 

       else 
        //do something else 

      } 
     }); 

Veya sadece isChecked() da kontrol etmek için kullanılabilir RadioButton'unuzun durumu. İşte

, kendi işlevini yürütebileceği bayrağına göre daha sonra

http://www.mkyong.com/android/android-radio-buttons-example/

Ve bir numune için bir bağlantıdır.

+0

Komplikasyona gerek yok, ancak karmaşık bir şey yaparsam yönteminizi kullanmanız kullanışlı olabilir. – IMustBeSomeone

11
 if(jRadioButton1.isSelected()){ 
      jTextField1.setText("Welcome"); 
     } 
     else if(jRadioButton2.isSelected()){ 
      jTextField1.setText("Hello"); 
     } 
0

Böyle anahtarı kullanabilirsiniz: Ayrıca düğme veya setOnCheckedChangeListener ile bir onClick işlevi uygulamak gerekecektir

XML Düzeni

<RadioGroup 
      android:id="@+id/RG" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <RadioButton 
       android:id="@+id/R1" 
       android:layout_width="wrap_contnet" 
       android:layout_height="wrap_content" 
       android:text="R1" /> 

      <RadioButton 
       android:id="@+id/R2" 
       android:layout_width="wrap_contnet" 
       android:layout_height="wrap_content" 
       android:text="R2" /> 
</RadioGroup> 

Ve JAVA Faaliyet

switch (RG.getCheckedRadioButtonId()) { 
     case R.id.R1: 
      regAuxiliar = ultimoRegistro; 
     case R.id.R2: 
      regAuxiliar = objRegistro; 
     default: 
      regAuxiliar = null; // none selected 
    } 

Gerekli işlevselliği elde etmek için işlev.

1

Eğer çözümlerini test espresso gerekirse şu şekildedir:

onView(withId(id)).check(matches(isChecked())); 

Hoşçakal

İlgili konular