2013-08-12 21 views
48

RadioGroup içinde üç RadioButton s ile bir kod parçası var. Benbir Toas t RadioButton değerini gösterecek şekilde ayarlamak istiyorum, ancak şu ana kadar aldım ne çalışmaz. RadioButton'un değerini nasıl alabilirim ve onu Toast'da görüntüleyebilir miyim?Seçilen telsizden değer alma

package com.example.toohelps; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    RadioGroup rg; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1); 
     final String value = 
      ((RadioButton)findViewById(rg.getCheckedRadioButtonId())) 
      .getText().toString(); 

     rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       Toast.makeText(getBaseContext(), value, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 

XML dosyası:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="152dp" > 

     <RadioButton 
      android:id="@+id/radio0" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Choose 1" /> 

     <RadioButton 
      android:id="@+id/radio1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Choose 2" /> 

     <RadioButton 
      android:id="@+id/radio2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Choose 3" /> 
    </RadioGroup> 
</RelativeLayout> 

cevap

81

Test ve çalışma şimdiden teşekkürler, bu benim kodudur. Eğer (herhangi bir ek Tamam düğmesine ya da bir şey kalmadan) radyo düğmelerinden birini seçiminde bazı iş yapmak istiyorsanız this

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.Toast; 

public class MyAndroidAppActivity extends Activity { 

    private RadioGroup radioGroup; 
    private RadioButton radioButton; 
    private Button btnDisplay; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    addListenerOnButton(); 

    } 

    public void addListenerOnButton() { 

    radioGroup = (RadioGroup) findViewById(R.id.radio); 
    btnDisplay = (Button) findViewById(R.id.btnDisplay); 

    btnDisplay.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

       // get selected radio button from radioGroup 
      int selectedId = radioGroup.getCheckedRadioButtonId(); 

      // find the radiobutton by returned id 
      radioButton = (RadioButton) findViewById(selectedId); 

      Toast.makeText(MyAndroidAppActivity.this, 
       radioButton.getText(), Toast.LENGTH_SHORT).show(); 

     } 

    }); 

    } 
} 

xml

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

     <RadioButton 
      android:id="@+id/radioMale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_male" 
      android:checked="true" /> 

     <RadioButton 
      android:id="@+id/radioFemale" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_female" /> 

    </RadioGroup> 
+5

Bu edilir:

Bu

benim çözümdür grubudur. – ajey

+1

Mükemmel !!! Cevap ... Kullanıcı tıkladığında, radyo düğmesi görünümü almak kolay! –

+1

Harika, teşekkürler çok, benim için çalışıyor. – EsmaeelE

30

durumda, .kodunuz edin iyi, küçük güncellendi.

public class MainActivity extends Activity { 

    RadioGroup rg; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1); 

     rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     { 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       switch(checkedId){ 
        case R.id.radio0: 
         // do operations specific to this selection 
         break; 
        case R.id.radio1: 
         // do operations specific to this selection 
         break; 
        case R.id.radio2: 
         // do operations specific to this selection 
         break; 
       } 
      } 
     }); 
    } 
} 
-1

RadioButtonlar dinamik olarak oluşturulduğunda, radyo düğmelerinin tanıtımıyla ilgili sorun yaşıyorum. Kimliği RadioButton.setId() kullanarak el ile ayarlamaya çalışırsanız, bu işe yaramaz. Benim için işe yarayan şey, radyo düğmeleri arasında yinelemek ve hangisinin işaretli olduğunu belirlemek için View.getChildAt() ve View.getParent()'u kullanmaktı. İhtiyacınız olan tek şey önce RadioGroup'u findViewById(R.id.myRadioGroup) aracılığıyla almak ve daha sonra çocukları ile tekrarlamaktır. Bulunduğunuz düğme üzerinde yinelediğinizde bunu bileceksiniz ve kontrol edilen düğme olup olmadığını belirlemek için RadioButton.isChecked()'u kullanabilirsiniz.

+0

Chinna'nın cevabını kontrol ederseniz, bu sayfa _is_ bağlantılı – MikeTheLiar

+1

@AbrahamUribe düzenlenmiş cevabımı inceleyebilir ve işaretlenmiş olandan daha iyi olup olmadığını görebilir misiniz? –

+0

@mikeTheLiar, doğru, orijinal cevabındaki bağlantıyı fark etmedim, bu benim hatam. –

3

sadece şey kontrol edilir, eğer -1 dönüş ise, Hava belirlemek için getCheckedRadioButtonId() işlevi kullandığınızda, tost programlı doldurma ve bir dizin almak isteyen herkes için

7
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) 
     { 
      radioButton = (RadioButton) findViewById(checkedId); 
      Toast.makeText(getBaseContext(), radioButton.getText(), Toast.LENGTH_SHORT).show(); 
     } 
    } 
    ); 
2

görünmesini önleyebilirsiniz, sen checkedId öğesinin etkinlik/parçaya döndüğünüzde değiştiğini ve bu radyo düğmelerini yeniden eklediğinizi fark edebilir. senin dinleyici içinde Sonra

for(int i = 0; i < myNames.length; i++) { 
     rB = new RadioButton(getContext()); 
     rB.setText(myNames[i]); 
     rB.setTag(i); 
     myRadioGroup.addView(rB,i); 
    } 

: Bu işleri

myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      RadioButton radioButton = (RadioButton) group.findViewById(checkedId); 
      int mySelectedIndex = (int) radioButton.getTag(); 
     } 
    }); 
1
int genid=gender.getCheckedRadioButtonId(); 
RadioButton radioButton = (RadioButton) findViewById(genid); 
String gender=radioButton.getText().toString(); 

Umut bu aşmanın bir yolu indeksi ile bir etiket ayarlamaktır. Çıktınızı dizgeye yukarıdaki şekilde dönüştürebilirsiniz. gender.getCheckedRadioButtonId(); - gender, RadioGroup'un idiğidir.

0

Chris'e çok teşekkürler. u bağlantıyı mykong ... yerine düğmesini kullanarak gelen gördük şey birden radyo id..for seçilen biz becerebiliyorolman

RadioGroup radgroup_opcionesEventos = null; 


private static String[] arrayEventos = { 
      "Congestión", "Derrumbe", "Accidente" 
    }; 
@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     radgroup_opcionesEventos = (RadioGroup)findViewById(R.id.rg_opciones_evento); 
     int i=0;//a.new.ln 
     for(String evento : arrayEventos) { 
      //RadioButton nuevoRadio = crearRadioButton(evento);//a.old.ln 
      RadioButton nuevoRadio = crearRadioButton(evento,i);//a.new.ln 
      radgroup_opcionesEventos.addView(nuevoRadio,i); 
     } 

     RadioButton primerRadio = (RadioButton) radgroup_opcionesEventos.getChildAt(0); 
     primerRadio.setChecked(true); 
} 

private RadioButton crearRadioButton(String evento, int n) 
    { 
     //RadioButton nuevoRadio = new RadioButton(this);//a.old.ln 
     RadioButton nuevoRadio = new RadioButton(getApplicationContext());//a.new.ln 

     LinearLayout.LayoutParams params = new RadioGroup.LayoutParams(
       RadioGroup.LayoutParams.WRAP_CONTENT, 
       RadioGroup.LayoutParams.WRAP_CONTENT); 
     nuevoRadio.setLayoutParams(params); 
     nuevoRadio.setText(evento); 
     nuevoRadio.setTag(evento); 
     return nuevoRadio; 
    } 


@Override 
    protected void onResume() 
    { 
     radgroup_opcionesEventos.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       RadioButton radioButton = (RadioButton) group.findViewById(checkedId); 
       //int mySelectedIndex = (int) radioButton.getTag(); 
       String mySelectedIndex = radioButton.getTag().toString(); 
      } 
     }); 
     super.onResume(); 
    } 
1
Radiogroup rgteam; 
String team; 
rgteam.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 
      RadioButton rb= (RadioButton) findViewById(checkedId); 
      team = rb.getText().toString(); 
     } 
    }); 
0
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 
     RadioButton radioButton = (RadioButton)group.findViewById(checkedId); 
    } 
}); 
İlgili konular