2013-12-16 28 views
6

私はをRadioButtonで動的に作成しており、デフォルトで1つのラジオボタンをオンにする必要があります。デフォルトのラジオボタンを設定します

私は私が持っている問題は、私は、実行時に、私はラジオで2つの確認のラジオボタンで終わるように、最初の1がチェックしたまま、別のラジオボタンを選択したときにということです両方radioButton.setChecked(true)radioButton.toggle();

を使用してこれをやりましたグループ。

誰もがこの問題を抱えており、それを解決する方法を知っていますか?

private RadioButton addRadioButton(String type, String price){ 
     RadioButton radio = new RadioButton(Order.this); 
     radio.setText(type + " (" + Utils.formatCurrency(price) + ")"); 
     radio.setTextAppearance(Order.this, R.style.portalCellTextStyle); 
     radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); 
     radio.setTag(price); 

     if(type.toLowerCase().equals("m")) 
      radio.toggle(); 

     return radio; 
    } 
+0

は、あなたがラジオグループ修正するためにラジオボタンを追加しますか? – ridvanzoro

答えて

1

あなただけ多分あなたは、チェックボックスを使用すべきか、代わりにボタンを切り替え、オンとオフを確認するための一つの無線ボックスを使用している場合。

http://developer.android.com/resources/tutorials/views/hello-formstuff.html

スクロールダウンし、チェックボックスを表示し、ボタンを切り替えます。

通常、ラジオを使用するときは、複数のラジオを使用し、それらのラジオを選択します。簡単、中、ハードのように。

あなたはラジオ・グループでそれを確認する必要があり

radio.setChecked(true); 
+0

私は両方を試してみましたが、両方ともボタンをチェックしましたが、別のものをチェックするとボタンはまだチェックされたままです – thehindutimes

+0

あなたはそれらをシングルラジオグループに追加していますか? – SilentKiller

0

radio.toggle(); 

を交換...

radiogroup.check(IdOfYourButton) 
0

はすでに確認ボタンをクリアするclearCheck()関数を使用します。これがあなたの問題を解決することを願っています。

0

OnCheckedChangeListener()を設定した後、ラジオグループのchecked()メソッドを直接呼び出して、何らかの理由ですぐに呼び出され、NPEがスローされたためです。

checked()メソッド呼び出しをOnCheckedChangeListener()の直前に移動して、今すぐ動作します。

radios.check(2); 
radios.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(RadioGroup group, int checkedId) { 

       } 
      }); 

private RadioButton addRadioButton(String type, String price){ 
     RadioButton radio = new RadioButton(Order.this); 
     radio.setText(type + " (" + Utils.formatCurrency(price) + ")"); 
     radio.setTextAppearance(Order.this, R.style.portalCellTextStyle); 
     radio.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); 
     radio.setTag(price); 

     if(type.toLowerCase().equals("m")) 
      radio.setId(2); 

     return radio; 
    } 
1

あなたは、単にこれを行うことができます。

JAVA:

radiogroup =(RadioGroup)findViewById(R.id.radiogroup); 
    radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1); 
    radiobutton2 =(RadioButton)findViewById(R.id.radiobutton2); 

    if(your first assessment){ 
     radiobutton1.setChecked(true); 
    }else{ 
     radiobutton2.setChecked(true); 
    } 

XML:私は遅刻が、同様の回答を検索する人のために知って

<RadioGroup 
     android:id="@+id/radiogroup" 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <RadioButton 
     android:id="@+id/radiobutton1" 
     android:checked="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

     <RadioButton 
     android:id="@+id/radiobutton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    </RadioGroup> 
7

私はこれが役に立ちます。

あなたがラジオグループにラジオボタンを追加すると、あなたのような親にそれを追加した後にチェックボタンを設定する必要がありますが:

RadioGroup radioGroup = new RadioGroup(getContext()) 
RadioButton radioButton = new RadioButton(getContext()); 
radioButton.setText("text"); 
mRadioGroup.addView(radioButton); 
radioButton.setChecked(true); 

ビューが親に追加する前にチェックされている場合、それができなくなりますそれをオフにします。最初の時点で画面がでGetviewメソッドの位置を確認してから条件を適用するロード

+0

最近同じ問題が発生しました。それが助けてくれてありがとう。 –

2

シンプル、:

if (position == 0) { 
    holder.act_delivery_rbtn.setChecked(true); 
} 
関連する問題