2010-03-04 18 views
28

शीर्षक के अनुसार, मैं एक रेडियो समूह में 3x3 रेडियो बटन के ग्रिड को समूहबद्ध करने का प्रयास कर रहा हूं। पिछले प्रश्न में मैंने पूछा कि रेडियो बटन के लिए एक समूह के अनुरूप होने के लिए उन्हें रेडियो समूह के तत्काल बच्चे होना चाहिए, जिनके साथ वे मेल खाते हैं। मैंने यह एक कठिन तरीका सीखा जब मैंने एक रेडियो समूह में एक संपूर्ण टेबल लेआउट (तालिका पंक्तियों में रेडियो बटन के साथ) को समाहित करने का प्रयास किया।रेडियो बटन के 3x3 ग्रिड को कैसे समूहित करें?

<TableLayout android:id="@+id/table_radButtons" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/title_radGroup_buffer"> 

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

       <RadioButton android:id="@+id/rad1" 
        android:text="Button1" 
        android:layout_width="105px" 
        android:layout_height="wrap_content" 
        android:textSize="13px"></RadioButton> 
       <RadioButton android:id="@+id/rad2" 
        android:text="Button2" 
        android:layout_width="105px" 
        android:textSize="13px" 
        android:layout_height="wrap_content"></RadioButton> 
       <RadioButton android:id="@+id/rad3" 
        android:text="Button3" 
        android:layout_width="105px" 
        android:textSize="13px" 
        android:layout_height="wrap_content"></RadioButton> 
      </RadioGroup> 
     </TableRow> 
     <TableRow> 
      <RadioGroup android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:id="@+id/radGroup1"> 
        <!-- snippet --> 
     </TableRow> 
     <!-- snippet ---> 
</TableLayout> 

जाहिर है मैं पहली बार नहीं सीखा क्योंकि मैं फिर से एक दीवार में भाग:

कि दीवार में चल रहा है, मैं निम्नलिखित की कोशिश की। मैं उम्मीद कर रहा था कि विभिन्न टेबल पंक्तियों में रेडियो बटन नोटिस करेंगे कि वे एक ही रेडियो समूह का हिस्सा थे (प्रत्येक समूह को एक ही आईडी दिया गया था) लेकिन यह काम नहीं करता था।

क्या कोई तरीका है कि मैं इन सभी बटनों को एक एकल समूह समूह में समूहित कर सकता हूं और अभी भी अपनी 3x3 संरचना (प्रत्येक पंक्ति में 3 पंक्तियां, 3 रेडियो बटन) बनाए रख सकता हूं?

उत्तर

54

वास्तव में यह है कि मुश्किल नहीं है, तो आप इस उदाहरण

/** 
* 
*/ 
package com.codtech.android.view; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.RadioButton; 
import android.widget.TableLayout; 
import android.widget.TableRow; 

/** 
* @author diego 
* 
*/ 
public class ToggleButtonGroupTableLayout extends TableLayout implements OnClickListener { 

    private static final String TAG = "ToggleButtonGroupTableLayout"; 
    private RadioButton activeRadioButton; 

    /** 
    * @param context 
    */ 
    public ToggleButtonGroupTableLayout(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @param context 
    * @param attrs 
    */ 
    public ToggleButtonGroupTableLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onClick(View v) { 
     final RadioButton rb = (RadioButton) v; 
     if (activeRadioButton != null) { 
      activeRadioButton.setChecked(false); 
     } 
     rb.setChecked(true); 
     activeRadioButton = rb; 
    } 

    /* (non-Javadoc) 
    * @see android.widget.TableLayout#addView(android.view.View, int, android.view.ViewGroup.LayoutParams) 
    */ 
    @Override 
    public void addView(View child, int index, 
      android.view.ViewGroup.LayoutParams params) { 
     super.addView(child, index, params); 
     setChildrenOnClickListener((TableRow)child); 
    } 


    /* (non-Javadoc) 
    * @see android.widget.TableLayout#addView(android.view.View, android.view.ViewGroup.LayoutParams) 
    */ 
    @Override 
    public void addView(View child, android.view.ViewGroup.LayoutParams params) { 
     super.addView(child, params); 
     setChildrenOnClickListener((TableRow)child); 
    } 


    private void setChildrenOnClickListener(TableRow tr) { 
     final int c = tr.getChildCount(); 
     for (int i=0; i < c; i++) { 
      final View v = tr.getChildAt(i); 
      if (v instanceof RadioButton) { 
       v.setOnClickListener(this); 
      } 
     } 
    } 

    public int getCheckedRadioButtonId() { 
     if (activeRadioButton != null) { 
      return activeRadioButton.getId(); 
     } 

     return -1; 
    } 
} 

में तरह TableLayout उपवर्ग और इस तरह एक लेआउट बनाने (बेशक आप इसे साफ करने के लिए की जरूरत है, लेकिन आप विचार आया)

<?xml version="1.0" encoding="utf-8"?> 
<com.codtech.android.view.ToggleButtonGroupTableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:id="@+id/radGroup1"> 
    <TableRow> 
      <RadioButton android:id="@+id/rad1" android:text="Button1" 
       android:layout_width="105px" android:layout_height="wrap_content" 
       android:textSize="13px" /> 
      <RadioButton android:id="@+id/rad2" android:text="Button2" 
       android:layout_width="105px" android:textSize="13px" 
       android:layout_height="wrap_content" /> 
      <RadioButton android:id="@+id/rad3" android:text="Button3" 
       android:layout_width="105px" android:textSize="13px" 
       android:layout_height="wrap_content" /> 
    </TableRow> 
    <TableRow> 
      <RadioButton android:id="@+id/rad1" android:text="Button1" 
       android:layout_width="105px" android:layout_height="wrap_content" 
       android:textSize="13px" /> 
      <RadioButton android:id="@+id/rad2" android:text="Button2" 
       android:layout_width="105px" android:textSize="13px" 
       android:layout_height="wrap_content" /> 
      <RadioButton android:id="@+id/rad3" android:text="Button3" 
       android:layout_width="105px" android:textSize="13px" 
       android:layout_height="wrap_content" /> 
    </TableRow> 
    <TableRow> 
      <RadioButton android:id="@+id/rad1" android:text="Button1" 
       android:layout_width="105px" android:layout_height="wrap_content" 
       android:textSize="13px" /> 
      <RadioButton android:id="@+id/rad2" android:text="Button2" 
       android:layout_width="105px" android:textSize="13px" 
       android:layout_height="wrap_content" /> 
      <RadioButton android:id="@+id/rad3" android:text="Button3" 
       android:layout_width="105px" android:textSize="13px" 
       android:layout_height="wrap_content" /> 
    </TableRow> 
</com.codtech.android.view.ToggleButtonGroupTableLayout> 
+1

मुझे इसे देखना होगा और देखें कि क्या मैं समझ सकता हूं कि आप क्या कर रहे हैं। धन्यवाद। – Fizz

+0

जब भी यह बदला जाता है तो चेक बटन आईडी कैसे प्राप्त करें? – wolverine

+0

धन्यवाद, यह पूरी तरह से काम किया। कॉपी, पेस्ट, किया। उत्तम सामग्री!! – muetzenflo

2

आपका एकमात्र विकल्प स्रोत कोड को RadioGroup पर ले जाना है और इसकी कार्यक्षमता को TableLayout या कुछ में दोहराने का प्रयास करना है। अन्यथा RadioButtons का 3x3 ग्रिड बनाने का कोई तरीका नहीं है। सौभाग्य से, RadioButton कक्षा को RadioGroup के बारे में पता नहीं है - सभी पारस्परिक-बहिष्करण तर्क RadioGroup में है। इसलिए, RadioGrid या कुछ बनाना संभव होना चाहिए ... लेकिन यह एक बहुत काम करने वाला है।

+0

तो ग्रिड व्यू जैसे कुछ का उपयोग करने का कोई तरीका नहीं है? – Fizz

+0

केवल तभी यदि आप इसे उपclass करते हैं और इसे रेडियो ग्रुप क्षमताओं देते हैं। – CommonsWare

2

RadioGroup निम्नलिखित तरीके से फैली हुई है ..

java.lang.Object 
↳ android.view.View 
    ↳ android.view.ViewGroup 
     ↳ android.widget.LinearLayout 
      ↳ android.widget.RadioGroup 

आप के लिए की जरूरत है एक ग्रिड लेआउट में रेडियो बटन की व्यवस्था करें, आपको GridLayout से विस्तारित अपना कस्टम कोड बनाने की आवश्यकता हो सकती है।

0

यह एक झुकाव है, लेकिन क्यों न केवल 9 रेडियो ग्रुप, या तीन क्षैतिज रेडियो ग्रुप का उपयोग तीन बटनों के साथ क्यों करें। प्रत्येक रेडियो समूह को ग्रिड व्यू, या सापेक्ष लेआउट आदि के साथ गठबंधन किया जा सकता है

रेडियोबटन के मानक ऑनचेकड चेंजलिस्टर का उपयोग करने के बजाय इसके बजाय कंपाउंडबटन से संबंधित एक (उसी नाम का) उपयोग करें।

फिर जब कभी भी रेडियोबटन दबाया जाता है तो आप रेडियो बटनों को अचयनित करने के लिए रेडियो समूहों का उपयोग कर सकते हैं। फिर प्रोग्रामेटिक रूप से क्लिक किए गए रेडियो बटन का चयन करें।

यह एक भयानक समाधान है, लेकिन यह काम करता है क्योंकि यह उम्मीद करता है।

नीचे कुछ उदाहरण कोड है जो मैं बटन के 2x2 लेआउट के साथ ऐसा करता था।

public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     updateList(Hurricane.ELEVATION_ALL); 

     watermarkAdapter = new WatermarkAdapter(getActivity(), R.layout.watermark_item, 
          relatedHurricanes); 

     setListAdapter(watermarkAdapter); 

     this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     this.getView().setBackgroundResource(R.drawable.splash_screen1); 


     getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 


     View v = this.getView(); 
     filterGroup1 = (RadioGroup)v.findViewById(R.id.filter_rg1); 
     filterGroup2 = (RadioGroup)v.findViewById(R.id.filter_rg2); 
     filterGroup3 = (RadioGroup)v.findViewById(R.id.filter_rg3); 
     filterGroup4 = (RadioGroup)v.findViewById(R.id.filter_rg4); 


     rb1 = (RadioButton) v.findViewById(R.id.first_radio_button);//All 
     rb2 = (RadioButton) v.findViewById(R.id.second_radio_button);//0-6 
     rb4 = (RadioButton) v.findViewById(R.id.third_radio_button);//6-12 
     rb3 = (RadioButton) v.findViewById(R.id.fourth_radio_button);//>=5 

     rb1.setOnCheckedChangeListener(this); 
     rb2.setOnCheckedChangeListener(this); 
     rb3.setOnCheckedChangeListener(this); 
     rb4.setOnCheckedChangeListener(this); 


    } 






@Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

     filterGroup1.clearCheck(); 
     filterGroup2.clearCheck(); 
     filterGroup3.clearCheck(); 
     filterGroup4.clearCheck(); 


     SharedPreferences prefs = PreferenceManager 
       .getDefaultSharedPreferences(this.getActivity()); 

     int cid = buttonView.getId(); 

     Editor editor  = prefs.edit(); 
     int elevation = Hurricane.ELEVATION_ALL; 
     //All 
     if(rb1.getId() == cid){ 
      elevation = Hurricane.ELEVATION_ALL; 
     } 
     //0-6 
     else if(rb2.getId() == cid){ 
      elevation = Hurricane.ELEVATION_6to12; 
     } 
     //6-12 
     else if(rb3.getId() == cid){  
      elevation = Hurricane.ELEVATION_0to6; 
     } 
     //>=12 
     else if(rb4.getId() == cid){ 
      elevation = Hurricane.ELEVATION_GT12; 
     } 

     update(StormFragment.NORMAL, elevation); 

    } 
5

ऊपर के बाद https://stackoverflow.com/a/2383978/5567009 जवाब मैं इस सवाल के लिए एक और समाधान मिल गया, मैं रेडियो समूह में तरह जांच कार्यक्षमता स्पष्ट करने के लिए समूह का राज्य है और यह भी कार्यक्षमता को बचाने के लिए कुछ अन्य अतिरिक्त कार्यशीलता, जैसे।

import android.content.Context; 
import android.os.Parcel; 
import android.os.Parcelable; 
import android.support.annotation.IdRes; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.RadioButton; 
import android.widget.TableLayout; 
import android.widget.TableRow; 

public class RadioGridGroup extends TableLayout implements View.OnClickListener { 

    private static final String TAG = "ToggleButtonGroupTableLayout"; 
    private int checkedButtonID = -1; 

    /** 
    * @param context 
    */ 
    public RadioGridGroup(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @param context 
    * @param attrs 
    */ 
    public RadioGridGroup(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onClick(View v) { 
     if (v instanceof RadioButton) { 
      int id = v.getId(); 
      check(id); 
     } 
    } 

    private void setCheckedStateForView(int viewId, boolean checked) { 
     View checkedView = findViewById(viewId); 
     if (checkedView != null && checkedView instanceof RadioButton) { 
      ((RadioButton) checkedView).setChecked(checked); 
     } 
    } 

    /* (non-Javadoc) 
    * @see android.widget.TableLayout#addView(android.view.View, int, android.view.ViewGroup.LayoutParams) 
    */ 
    @Override 
    public void addView(View child, int index, 
         android.view.ViewGroup.LayoutParams params) { 
     super.addView(child, index, params); 
     setChildrenOnClickListener((TableRow) child); 
    } 


    /* (non-Javadoc) 
    * @see android.widget.TableLayout#addView(android.view.View, android.view.ViewGroup.LayoutParams) 
    */ 
    @Override 
    public void addView(View child, android.view.ViewGroup.LayoutParams params) { 
     super.addView(child, params); 
     setChildrenOnClickListener((TableRow) child); 
    } 


    private void setChildrenOnClickListener(TableRow tr) { 
     final int c = tr.getChildCount(); 
     for (int i = 0; i < c; i++) { 
      final View v = tr.getChildAt(i); 
      if (v instanceof RadioButton) { 
       v.setOnClickListener(this); 
      } 
     } 
    } 


    /** 
    * @return the checked button Id 
    */ 
    public int getCheckedRadioButtonId() { 
     return checkedButtonID; 
    } 


    /** 
    * Check the id 
    * 
    * @param id 
    */ 
    public void check(@IdRes int id) { 
     // don't even bother 
     if (id != -1 && (id == checkedButtonID)) { 
      return; 
     } 
     if (checkedButtonID != -1) { 
      setCheckedStateForView(checkedButtonID, false); 
     } 
     if (id != -1) { 
      setCheckedStateForView(id, true); 
     } 
     setCheckedId(id); 
    } 

    /** 
    * set the checked button Id 
    * 
    * @param id 
    */ 
    private void setCheckedId(int id) { 
     this.checkedButtonID = id; 
    } 

    public void clearCheck() { 
     check(-1); 
    } 

    @Override 
    protected void onRestoreInstanceState(Parcelable state) { 
     if (!(state instanceof SavedState)) { 
      super.onRestoreInstanceState(state); 
      return; 
     } 

     SavedState ss = (SavedState) state; 
     super.onRestoreInstanceState(ss.getSuperState()); 

     this.checkedButtonID = ss.buttonId; 
     setCheckedStateForView(checkedButtonID, true); 
    } 

    @Override 
    protected Parcelable onSaveInstanceState() { 
     Parcelable superState = super.onSaveInstanceState(); 
     SavedState savedState = new SavedState(superState); 
     savedState.buttonId = checkedButtonID; 
     return savedState; 
    } 

    static class SavedState extends BaseSavedState { 
     int buttonId; 

     /** 
     * Constructor used when reading from a parcel. Reads the state of the superclass. 
     * 
     * @param source 
     */ 
     public SavedState(Parcel source) { 
      super(source); 
      buttonId = source.readInt(); 
     } 

     /** 
     * Constructor called by derived classes when creating their SavedState objects 
     * 
     * @param superState The state of the superclass of this view 
     */ 
     public SavedState(Parcelable superState) { 
      super(superState); 
     } 

     @Override 
     public void writeToParcel(Parcel out, int flags) { 
      super.writeToParcel(out, flags); 
      out.writeInt(buttonId); 
     } 

     public static final Parcelable.Creator<SavedState> CREATOR = 
       new Parcelable.Creator<SavedState>() { 
        public SavedState createFromParcel(Parcel in) { 
         return new SavedState(in); 
        } 

        public SavedState[] newArray(int size) { 
         return new SavedState[size]; 
        } 
       }; 
    } 
} 

और के रूप में

<com.test.customviews.RadioGridGroup xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_contact" 
    android:layout_height="wrap_content"> 

    <TableRow android:layout_marginTop="@dimen/preview_five"> 

     <RadioButton 
      android:id="@+id/rad1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button1" /> 

     <RadioButton 
      android:id="@+id/rad2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button2" /> 
    </TableRow> 

    <TableRow android:layout_marginTop="@dimen/preview_five"> 

     <RadioButton 
      android:id="@+id/rad3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button3" /> 

     <RadioButton 
      android:id="@+id/rad4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button4" /> 
    </TableRow> 

    <TableRow android:layout_marginTop="@dimen/preview_five"> 

     <RadioButton 
      android:id="@+id/rad5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button5" /> 

     <RadioButton 
      android:id="@+id/rad6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button6" /> 
    </TableRow> 

    <TableRow android:layout_marginTop="@dimen/preview_five"> 

     <RadioButton 
      android:id="@+id/rad7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button7" /> 

     <RadioButton 
      android:id="@+id/rad8" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button8" /> 
    </TableRow> 
</com.test.customviews.RadioGridGroup> 

किसी भी अन्य सुधार के लिए इस प्रकार एक्सएमएल में इस का उपयोग करें, टिप्पणी कृपया।

+0

किसी भी जावा में एक संपादन का सुझाव दूंगा पैकेज आप इसे रख सकते हैं। –

0

मैं एक के लिए नेस्टेड रेडियो ग्रुप के लिए जाना होगा। रूट रेडियो ग्रुप में वर्टिकल ओरिएंटेशन होगा और इसके बच्चे क्षैतिज अभिविन्यास वाले तीन रेडियो ग्रुप होंगे।

<RadioGroup 
    android:orientation="vertical"> 
    <RadioGroup 
     android:id="@+id/rg_1" 
     android:orientation="horizontal"> 
     <RadioButton /> 
     <RadioButton /> 
     <RadioButton /> 
    </RadioGroup> 
    <RadioGroup 
     android:id="@id/rg_2" 
     android:orientation="horizontal"> 
     <RadioButton /> 
     <RadioButton /> 
     <RadioButton /> 
    </RadioGroup> 
    <RadioGroup 
     android:id="@+id/rg_3" 
     android:orientation="horizontal"> 
     <RadioButton /> 
     <RadioButton /> 
     <RadioButton /> 
    </RadioGroup> 
</RadioGroup> 

chield RadioGroups से प्रत्येक एक आईडी जो जावा सत्यापन विधि के अंदर एक RadioGroup वस्तु द्वारा बुलाया जाएगा होगा। इस तरह:

RadioGroup rg_1 = (RadioGroup) findViewById(R.id.rg_1); 
RadioGroup rg_2 = (RadioGroup) findViewById(R.id.rg_2); 
RadioGroup rg_3 = (RadioGroup) findViewById(R.id.rg_3); 
अब

बस स्विच मामले के अंदर clearCheck() का उपयोग करके आप अन्य दो RadioGroups की जांच साफ कर सकते हैं। इस तरह:

case R.id.radioButton_1: 
      if (checked) { 
       rg_2.clearCheck(); 
       rg_3.clearCheck(); 
      } 
      break; 
संबंधित मुद्दे