2017-04-24 52 views
0

我爲Radio Button創建了一個自定義視圖像這樣。 如何從自定義視圖中獲取RadioButton的值,並且一次只想選擇一個單選按鈕。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

通過值,你的意思是與單選按鈕相關的文字? –

+0

是的。我已經從服務器檢索數據,但無法獲取按鈕單擊事件 –

+1

上已選中的radioButton的值,您可以使用'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()); 


       } 

      } 
     }); 

或者你可以得到像這樣的價值manuel;

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

你可以搜索'持有人',也許它可以工作。或者你可以把價值發送到公共課,你可以得到,使用你想要的地方。 –

+0

我不認爲它會工作。 –

0

你需要把單選按鈕單選組像下面的xml文件

<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

我一直在使用自定義視圖,所以它不起作用,因爲單選按鈕是根據數據創建的 –

相關問題