0

嘿,伙计们有一个使用单选按钮的应用程序,如下代码

        default_mode =(RadioButton)findViewById(R.id.default_mode);
        warn_mode =(RadioButton)findViewById(R.id.warn_mode);
        grey_mode =(RadioButton)findViewById(R.id.grey_mode);
        QueGroup1 =(RadioGroup)findViewById(R.id.QueGroup1);
 QueGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(RadioGroup rg, int checkedId) {
        // TODO Auto-generated method stub
        for(int i=0; i<rg.getChildCount(); i++) { 

            //RadioButton btn = (RadioButton) rg.getChildAt(i); 
            if(default_mode.getId() == checkedId) { 
                default_method();
                colorTouched();

                return; 
            } 
            else if(warn_mode.getId() == checkedId) 
            { 
                warn_method();

                return;
            }
            else if(grey_mode.getId() == checkedId){
                grey_method();

                return;
            }
        }
    }
});

问题是当我在 default_mode 上选择然后在 warn_mode 上选择

名为 colorTouched() 的方法;仍在工作。我真正想知道的是如何从其他单选按钮中停止该方法。前任。如果我选择 warn_mode 方法 warn_method() 必须只工作。

提前致谢 :)))

4

1 回答 1

1

试过你的代码。不明白 for 循环在那里做什么,所以我删除了那个。

事情似乎按预期进行。你的布局可能有问题吗?

这是我使用的代码。

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <RadioGroup android:id="@+id/que_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:id="@+id/default_mode"
            android:text="Default Mode" android:checked="true"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:id="@+id/warn_mode"
            android:text="Warn Mode"></RadioButton>
        <RadioButton android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:id="@+id/grey_mode"
            android:text="Grey Mode"></RadioButton>
    </RadioGroup>
</LinearLayout>  

Java代码

package com.test.radiogrouptest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RadioGroupTestActivity extends Activity {
    public static final String TAG = "RGTA";
    RadioGroup queRG;
    RadioButton defaultModeRB, warnModeRB, greyModeRB;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        queRG = (RadioGroup) findViewById(R.id.que_group);
        defaultModeRB = (RadioButton) findViewById(R.id.default_mode);
        warnModeRB = (RadioButton) findViewById(R.id.warn_mode);
        greyModeRB = (RadioButton) findViewById(R.id.grey_mode);
        queRG.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup rg, int checkedId) {
                if (defaultModeRB.getId() == checkedId) {
                    defaultMethod();
                    colorTouched();
                    return;
                } else if (warnModeRB.getId() == checkedId) {
                    warnMethod();
                    return;
                } else if (greyModeRB.getId() == checkedId) {
                    greyMethod();
                    return;
                }
            }
        });
    }

    public void defaultMethod() {
        Log.d("TAG", "defaultMethod");
    }

    public void colorTouched() {
        Log.d("TAG", "colorTouched");
    }

    public void warnMethod() {
        Log.d("TAG", "warnMethod");
    }

    public void greyMethod() {
        Log.d("TAG", "greyMethod");
    }
}
于 2011-07-04T16:57:59.597 回答