Radio Group Android保持默认状态

时间:2016-01-05 17:28:37

标签: android button default radio

我在谷歌和StackOverflow上搜索任何解决方案,我发现它们很多,但我的情况都没有。

我有3个RadioButtons,因为它应该只有一个可选。

现在我想知道当我按下“访问”按钮时选择了哪个单选按钮。 我调用了VisitButton的函数,但是当我尝试读取RadioButton时,结果总是一样的。

我选择哪一个并不重要,它总是选择默认按钮。

我尝试了各种不同的方法,onChangeCheckListener,onClickListener,但它们似乎都不起作用。

我做错了什么?

这是我的代码片段:

MicrolabB  = (RadioButton) findViewById(R.id.Microlab);
        MoodleB = (RadioButton) findViewById(R.id.Moodle);
        ISAB = (RadioButton) findViewById(R.id.ISA);
        PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
        VISIT = (Button) findViewById(R.id.Visit);
        MicrolabB.setOnClickListener(this);
        ISAB.setOnClickListener(this);
        MoodleB.setOnClickListener(this);

    public void VisitWebPage(View v){
        if(v.getId()==R.id.Visit){

            if(MicrolabB.isChecked())
                ApduNFC.Portal =1;
            if(ISAB.isChecked())
                ApduNFC.Portal=2;
            if(MoodleB.isChecked())
                ApduNFC.Portal=3;

            Intent intent = new Intent(Start.this,ApduNFC.class);
            startActivity(intent);
        }

    }

我的XML:

 <RadioGroup
            android:id="@+id/PortalSelect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true" >

            <RadioButton
                android:id="@+id/Microlab"
                android:layout_width="133dp"
                android:layout_height="wrap_content"
                android:text="Microlab"
                android:clickable="true"
                 />

            <RadioButton
                android:id="@+id/ISA"
                android:layout_width="126dp"
                android:layout_height="wrap_content"
                android:text="IS-A"
                android:clickable="true"
                android:checked="true" />

            <RadioButton
                android:id="@+id/Moodle"
                android:layout_width="116dp"
                android:layout_height="wrap_content"
                android:text="Moodle"
                android:clickable="true" />
        </RadioGroup>

        <Button
            android:id="@+id/Visit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="52dp"
            android:onClick="VisitWebPage"
            android:text="Visit" />

3 个答案:

答案 0 :(得分:0)

首先,在这个例子中,你肯定不需要RadioButtons上的监听器。

获取所选按钮的最佳选项来自PortalSelect.getCheckedRadioButtonId(). 您将获得RadioButton的ID。

不要忘记检查id是否存在;默认情况下,RadioGroup初始化时没有选中任何RadioButtons;

请参阅Google RadioGroup网站: http://developer.android.com/reference/android/widget/RadioGroup.html

答案 1 :(得分:0)

VisitWebPage首先添加PortalSelect.getCheckedRadioButtonId()

public void VisitWebPage(View v){
    if(v.getId()==R.id.Visit){

        int selectedId = PortalSelect.getCheckedRadioButtonId();

        if(selectedId == MicrolabB.getId())
            Log.d("RB Selected","1");
        if(selectedId ==ISAB.getId())
            Log.d("RB Selected","2");
        if(selectedId ==MoodleB.getId())
            Log.d("RB Selected","3");

        //Intent intent = new Intent(Start.this,ApduNFC.class);
        //startActivity(intent);
    }

}

答案 2 :(得分:0)

解决了问题。

public void VisitWebPage(View v){
        final RadioGroup PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
        int ID = PortalSelect.getCheckedRadioButtonId();
        if(v.getId()==R.id.Visit){
        int rb1Id = MicrolabB.getId();
            if(ID == MicrolabB.getId())
                Log.d("RB Selected","1");
            if(ID == ISAB.getId())
                Log.d("RB Selected","2");
            if(ID == MoodleB.getId())
                Log.d("RB Selected","3");   

            Intent intent = new Intent(Start.this,ApduNFC.class);
            startActivity(intent);
        }
}

我放入线后

final RadioGroup PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
在VisitWebPage函数中

一切似乎都有效。我不知道为什么,因为我之前在这堂课中定义了Radio Group。但我很高兴它现在正在运作。

相关问题