Android 编程中处理单选按钮事件的问题

发布于 2024-11-18 04:30:48 字数 1218 浏览 0 评论 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() 必须只能工作。

提前致谢 :)))

Hey guys have an application with using radio button as following codes

        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;
            }
        }
    }
});

The problem is when I selected on default_mode then selected on warn_mode

the method named colorTouched(); is still working. What I really want to know is how to stop the method from other's radio button. Ex. If I select warn_mode the method warn_method() must working only.

Thanks in advance :)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-11-25 04:30:48

试过你的代码。不明白 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");
    }
}

Tried your code. Did not understand what the for loop is doing there so I removed that one.

Things seems to work a expected. Is there maybe something wrong with your layout?

Here is the code that I worked with.

XML-Code

<?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 -code

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文