开发者

Problem with handling event on radio buttons in Android programming

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(gre开发者_高级运维y_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 :)))


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜