【问题标题】:not able to unchecked the already checked radio button in dynamic radio group无法取消选中动态单选组中已选中的单选按钮
【发布时间】:2013-08-23 19:01:05
【问题描述】:

如果我第一次设置一个单选按钮,它工作正常。但是如果我通过调用 .setChecked(false); 取消选择它;然后,以后即使我尝试通过调用 setChecked(true) 将其选中,也不会取消选择前一个。

    private void radiotype() {
    count = //changed every time.
    LinearLayout llques = (LinearLayout) mview.findViewById(R.id.llrbgRBD);
    RadioGroup group = new RadioGroup(context);
    group.setOrientation(RadioGroup.VERTICAL);
    final RadioButton[] rb = new RadioButton[count];
    List<String[]> ans = getAnswerList.getAns();

    for (int j = 0; j < count; j++) {

        rb[j] = new RadioButton(context);
        rb[j].setVisibility(View.VISIBLE);
        rb[j].setText("`enter code here`hi");
        String a = rb[j].getText().toString();`enter code here`
        Log.e("getAnswerList===a", "getAnswerList===>a" + a);
        Log.e("getAnswerList", "getAnswerList===>" + ans.get(index)[0]);
        if (a.equalsIgnoreCase(ans.get(index)[0])) {
            rb[j].setChecked(true);
        }
        rb[j].setTextColor(Color.BLACK);
        rb[j].setButtonDrawable(R.drawable.custom_radio_button);
        group.addView(rb[j]);
    }
    llques.removeAllViews();
    llques.addView(group);
    group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            //int c = count;
            for(int i=0;i<count;i++){
                rb[i].setChecked(false);
            }
            //
            Log.v("id",""+checkedId);
            for (int i = 0; i < count; i++) {
                if (rb[i].getId() == checkedId){
                    rb[i].setChecked(true);
                } 
            }
        }
    });

【问题讨论】:

    标签: android radio-button unchecked


    【解决方案1】:

    我知道这已经很老了,但我也遇到了同样的问题。解决方案是通过 addView() 将RadioButton 添加到RadioGroup 之后更改其状态。我想这也是 BAKUS 试图通过他的示例代码表达的意思。

    【讨论】:

      【解决方案2】:

      RadioGroup 确保仅选中一个 RadioButton 的方式是基于这些 RadioButton 的 ID。
      尝试在你的第一个 for 循环中添加它:

      int uniqueId = j; // or whatever unique in this RadioGroup
      rb[j].setId(uniqueId);
      

      【讨论】:

        【解决方案3】:

        在你的 xml 中使用它。

        <RadioGroup
                        android:id="@+id/status_group"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:orientation="vertical"
                         android:layout_margin="5dp"
                         >
                     <RadioButton
                         android:id="@+id/open_radio"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:text="Open"
                         />
                    <RadioButton
                        android:id="@+id/close_radio"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:text="Closed"
                         />
                    <RadioButton
                        android:id="@+id/all_radio"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:text="Both"
                         />
        
                    </RadioGroup>
        

        然后在java中:

         final RadioGroup status_group = (RadioGroup) findViewById(R.id.status_group);
        
                //--    By default if you want open button to be checked, you can do that by using
                    status_group.check(R.id.open_radio);
        
                    status_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) { 
        
        
                            RadioButton radioButton = (RadioButton) findViewById(checkedId);
                            status_group.check(checkedId);
        
                            radio_status = radioButton.getText().toString().trim();
                            Log.v("radio_text--", radio_status);
                        }
                    });
        

        【讨论】:

        • 动态单选按钮和单选按钮组怎么做
        【解决方案4】:
        public void getqlist(int comp) 
        {   
            LinearLayout ll = (LinearLayout) findViewById(R.id.variable);
            ll.removeAllViews();
            xfiche=(ficheflag)?(fiche+1):fichetempo;
            String titre="Formulaire "+xfiche+"- Question "+(comp+1)+"/"+taille;
            TextView titreView =(TextView) findViewById(R.id.titre);
            titreView.setText(titre);    
            int ii= Integer.parseInt(quest[comp][3].toString());
            RadioButton[] rb = new RadioButton[ii];
            RadioGroup rg = new RadioGroup(this); 
            rg.setOrientation(RadioGroup.VERTICAL);
            int isel=Integer.parseInt(resp[comp][2].toString())-1;
            int bid=254211; // You give any integer ID
            for(int i=0; i<ii; i++){
                rb[i]  = new RadioButton(this);
                rb[i].setText(quest[comp][i+5].toString());
                if (i==isel)
                    rb[i].setId(bid); // You capure the previously selected radio button
                rg.addView(rb[i]);
            }      
            ll.addView(rg);//you add the whole RadioGroup to the layout
            RadioButton tb=(RadioButton) findViewById(bid); // find the button by the Id 
            tb.toggle();   // toggle it after diplaying the group    
        
        }
        

        【讨论】:

        • 您最好稍微解释一下您的答案,而不仅仅是粘贴代码。
        • 正如 paqogomez 所说,您能否为这个答案提供一些背景信息?就目前而言,它不是很有帮助。
        【解决方案5】:

        这样做 -

        if (a.equalsIgnoreCase(ans.get(index)[0])) {
                    rb[j].setChecked(true);
                }
        

        之后

        group.addView(rb[j]);
        

        而不是在 addView 之前检查它。

        让它像 -

        private void radiotype() {
        count = //changed every time.
        LinearLayout llques = (LinearLayout) mview.findViewById(R.id.llrbgRBD);
        RadioGroup group = new RadioGroup(context);
        group.setOrientation(RadioGroup.VERTICAL);
        final RadioButton[] rb = new RadioButton[count];
        List<String[]> ans = getAnswerList.getAns();
        
        for (int j = 0; j < count; j++) {
        
            rb[j] = new RadioButton(context);
            rb[j].setVisibility(View.VISIBLE);
            rb[j].setText("`enter code here`hi");
            String a = rb[j].getText().toString();`enter code here`
            Log.e("getAnswerList===a", "getAnswerList===>a" + a);
            Log.e("getAnswerList", "getAnswerList===>" + ans.get(index)[0]);
        
            rb[j].setTextColor(Color.BLACK);
            rb[j].setButtonDrawable(R.drawable.custom_radio_button);
            group.addView(rb[j]);
            if (a.equalsIgnoreCase(ans.get(index)[0])) {
                rb[j].setChecked(true);
            }
        }
        llques.removeAllViews();
        llques.addView(group);
        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected
                //int c = count;
                for(int i=0;i<count;i++){
                    rb[i].setChecked(false);
                }
                //
                Log.v("id",""+checkedId);
                for (int i = 0; i < count; i++) {
                    if (rb[i].getId() == checkedId){
                        rb[i].setChecked(true);
                    } 
                }
            }
        });
        

        对我也有用

        【讨论】:

          猜你喜欢
          • 2012-06-01
          • 2016-05-28
          • 2013-10-14
          • 1970-01-01
          • 2014-02-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-23
          相关资源
          最近更新 更多