2017-04-24 9 views
0

나는 라디오 버튼에 대한 사용자 정의보기를 만들었습니다. 어떻게 사용자 정의보기에서 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

으로 Value, 라디오 버튼과 관련된 텍스트를 의미합니까? –

+0

예. 서버에서 데이터를 검색했지만 버튼 클릭 이벤트 –

+1

에서 checked 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

'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> 

다음 XML 파일에 아래와 같이 라디오 그룹의 라디오 버튼을 설정 할 필요가

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

사용자 정의보기를 사용하여 데이터에 따라 라디오 단추가 만들어 지므로 작동하지 않습니다. –