Android 编程中处理单选按钮事件的问题
嘿,大家有一个使用单选按钮的应用程序,如下代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试过你的代码。不明白 for 循环在那里做什么,所以我删除了那个。
事情似乎按预期进行。您的布局可能有问题吗?
这是我使用的代码。
XML 代码
Java 代码
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
Java -code