2017-04-24 18 views
0

ラジオボタンのカスタムビューを作成しました。 カスタムビューからRadioButtonの値を取得するには、一度に1つのラジオボタンを選択する必要があります。Android:カスタムビューからRadioButtonの値を取得する

public void getData(){ 
    cappingLayout.removeAllViews(); 
    for (Capping capping: productList.getCappingList()){ 
     if (getActivity()!=null) { 
      View view = LayoutInflater.from(getActivity()).inflate(R.layout.product_capping, cappingLayout, false); 
      cappingRadio = (RadioButton) view.findViewById(R.id.fragment_product_detail_capping); 
      cappingRadio.setText(capping.getCapping_type()); 
      cappingLayout.addView(view); 
     } 
    } 
} 

Product_capping.xml

<RadioButton 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/fragment_product_detail_capping" 
    style="@style/Widget.AppCompat.CompoundButton.CheckBox" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textStyle="bold" 
    tools:text="Normal"/> 

私はボタンをクリックした後、ラジオボタンの値を取得したいです。

+0

を選択したラジオボタンを取得することができますValue、ラジオボタンに関連付けられたテキストを意味しますか? –

+0

はい。私はサーバーからデータを取得していますが、ボタンクリックイベントでチェックされたradioButtonの値を得ることができません –

+1

'radioButton.isChecked'を使ってチェックされているかどうかを確認したり、' radioButton.getText'ラジオボタン –

答えて

0

これを試すことができます。

rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       // TODO Auto-generated method stub 
       int childCount = group.getChildCount(); 
       for (int x = 0; x < childCount; x++) { 
        RadioButton btn = (RadioButton) group.getChildAt(x); 

        if (btn.getId() == checkedId) { 

         System.out.println(btn.getText().toString()); 


       } 

      } 
     }); 

または、このような値を得ることができます。 xmlファイルで使用すると、以下のようなグループのラジオボタンを配置する必要があり

public void hesap(View view) { 

    if(rb1.isChecked()) 
     hsp.setText("rb1 Value"); 
    if(rb2.isChecked()) 
     hsp.setText("rb2 Value"); 
    if(rb3.isChecked()) 
     hsp.setText("rb3 Value"); 
    } 
+0

それは働かなかった。 –

+0

あなたは 'holder'を検索できますか、多分それは働くことができます。あるいは、値をパブリッククラスに送ることができます。そして、あなたは得ることができ、あなたが望むところで使用することができます。 –

+0

私はうまくいくとは思わない。 –

0

<RadioGroup 
       android:id="@+id/rg_gender" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="horizontal"> 
       <RadioButton 
         android:id="@+id/rb_male" 
         android:gravity="center" 
         android:text="@string/male" 
         android:textColor="@color/col_white" 
         android:textSize="16dp" /> 
        <RadioButton 
         android:id="@+id/rb_female" 
         android:text="@string/female" 
         android:textColor="@color/col_white" 
         android:textSize="16dp" /> 
     </RadioGroup> 

、あなたがして、Javaファイルに以下のようなラジオ・グループから

rg_gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { 
       if (checkedId == R.id.rb_male) { 
        gender = "male"; 
       } else { 
        gender = "female"; 
       } 
      } 
     }); 
+0

カスタムビューを使用していますので、データに応じてラジオボタンが作成されるため、表示されません –

関連する問題