【问题标题】:radiobutton in android doesn't workandroid中的单选按钮不起作用
【发布时间】:2017-02-02 20:47:50
【问题描述】:

在这个程序中,我使用单选按钮。目前我在使用 2 号按钮 (button2) 时遇到了一些问题,事实上当它被按下时,什么也没有发生!

当我按下button2 时,它应该在“TextView”中显示每个单选按钮的名称 (而button1 用于清除选择单选按钮)。 任何人都可以帮助我或给我任何提示吗?谢谢。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;    

public class RadiobuttonActivity extends Activity {     

    Button      button1;
    Button      button2;
    TextView    textView1;
    RadioButton r1;
    RadioButton r2;
    RadioButton r3;     

    RadioGroup  radioGroup1;     

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        textView1 = (TextView) findViewById(R.id.textView1);
        radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
        r1 = (RadioButton) findViewById(R.id.r1);
        r2 = (RadioButton) findViewById(R.id.r2);
        r3 = (RadioButton) findViewById(R.id.r3);     

        button1.setOnClickListener(new OnClickListener() {     

            @Override
            public void onClick(View arg0) {
                radioGroup1.clearCheck();
                textView1.setText("AllUnChecked");     
            }
        });     

        button2.setOnClickListener(new OnClickListener() {     

            @Override
            public void onClick(View arg0) {
                int selectedid = radioGroup1.getCheckedRadioButtonId();
                switch (selectedid) {
                    case 0:
                        textView1.setText("Red");
                        break;
                    case 1:
                        textView1.setText("Green");
                        break;
                    case 2:
                        textView1.setText("Black");
                        break;
                }
            }
        });     
    }
}

这是 main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >



            <RadioButton
                android:id="@+id/r1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Red" />



            <RadioButton
                android:id="@+id/r2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Green" />



            <RadioButton
                android:id="@+id/r3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Black" />

        </RadioGroup>


        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AllUnchecked" />


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Witch radio button is ckecked?" />


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text View"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

and this is main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >



        <RadioButton
            android:id="@+id/r1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Red" />



        <RadioButton
            android:id="@+id/r2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Green" />



        <RadioButton
            android:id="@+id/r3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Black" />

    </RadioGroup>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AllUnchecked" />


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Witch radio button is ckecked?" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text View"
        android:textAppearance="?android:attr/textAppearanceLarge" />

【问题讨论】:

  • 请显示 XML 布局。
  • 这里的代码没有明显的问题。我建议在你的事件监听器中设置一个断点,并使用 Android Studio 中的调试来确定发生了什么。
  • 这是 main.xml :
  • @Alien_x 您不应该发布您的附加代码作为答案。相反,您应该单击编辑然后添加它。
  • 请编辑您原来的问题。

标签: java android radio-button


【解决方案1】:

this SO post中所述

代替使用:

int selectedid = radioGroup1.getCheckedRadioButtonId();

使用这个:

 int selectedid = radioGroup1.indexOfChild(findViewById(radioGroup1.getCheckedRadioButtonId()));

【讨论】:

  • 通过单击左侧的标志来标记它。谢谢
猜你喜欢
  • 2017-07-13
  • 2018-05-04
  • 1970-01-01
  • 2018-01-17
  • 2013-06-22
  • 2012-05-17
  • 2016-08-30
  • 2012-01-29
  • 2015-11-15
相关资源
最近更新 更多