Android:使用单选按钮更改文本视图的内容

时间:2017-02-27 00:39:13

标签: java android

所以,我正在尝试编写代码,显示Radio Button点击注册。理想情况下,当选择单选按钮时,TextView的内容将发生变化。首先,我有一个“北方”的单选按钮。选中“北”时,TextView的内容将变为“北”。我知道有动作听众参与,但我不熟悉Java。这肯定会流行我的Java樱桃。话虽这么说,我写的代码不起作用。任何人都可以告诉我,如果我走在正确的轨道上,或提供一些建议?请注意,这不适用于课堂作业。这是一个非常开放的课程项目,我正与另一个人合作。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
public void onRadioButtonClicked(View v){
  TextView text = (TextView)findViewById(R.id.text);

    RadioButton N = (RadioButton)  findViewById(R.id.north);

    //evaluates 'checked' value of radio button
    boolean  checked = ((RadioButton) v).isChecked();

 if(N.isChecked () ){

     text.setText("N");
 }
}

}

4 个答案:

答案 0 :(得分:2)

要正确使用RadioButton,您最好将一堆RadioButton分组到一个名为RadioGroup的集合中。

<RadioGroup
    android:id="@+id/rg1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/rg1_rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="North" />

    <RadioButton
        android:id="@+id/rg1_rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="South" />

    <RadioButton
        android:id="@+id/rg1_rb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Whatever" />
</RadioGroup>


关键部分是您必须为每个android:id设置唯一RadioButton,否则他们将无法工作!

接下来,从您的XML中找到RadioButton

RadioButton rb1, rb2, rb3;

rb1 = (RadioButton) findViewById(R.id.rg1_rb1);
rb2 = (RadioButton) findViewById(R.id.rg1_rb2);
rb3 = (RadioButton) findViewById(R.id.rg1_rb3);

最后,准备一个RadioButton.OnClickListener类实例并将其附加到RadioButton

View.OnClickListener optionOnClickListener
            = new View.OnClickListener() {

    public void onClick(View v) {
        TextView tv = (TextView) findViewById(R.id.textview);
        String str = null;

        // you can simply copy the string of clicked button.            
        str = ((RadioButton)v).getText().toString();
        tv.setText(str);

        // to go further with check state you can manually check each radiobutton and find which one is checked.
        if(rb1.isChecked()) {
            // do something
        }
        if(rb2.isChecked()) {
            // do something
        }
        if(rb3.isChecked()) {
            // do something
        }
    }
};

rb1.setOnClickListener(optionOnClickListener);
rb2.setOnClickListener(optionOnClickListener);
rb3.setOnClickListener(optionOnClickListener);

// check rb1 by default, if you want.
rb1.setChecked(true);


增加:

对不起,但我无法理解我的答案的编辑版本,因为在View.OnClickLister.OnClick()中调用setOnClickListener()对我来说有点奇怪。
所以我回到原来的答案。

答案 1 :(得分:1)

尝试

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.your_radio_group_id);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.north ;
                // set text North for your textview here
                break;
            case R.id.another_radio_button_id:
                // do something
                break;
        }
    }
});

答案 2 :(得分:0)

检查您的xml文件。单选按钮必须在收音机组内才能完美地工作。

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

        <RadioButton
            android:id="@+id/n"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="I am a Blood Donor" />

        <RadioButton
            android:id="@+id/s"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             />
    </RadioGroup>

然后更改您的java文件

mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if (i == R.id.s) {
                //Your action
            } else if (i == R.id.n) {
                //Your action
            }
        }
    });

它会工作.. :) :))

答案 3 :(得分:0)

好的,所以制作&#39; onCheckedChange&#39; onClick事件似乎可以解决问题。感谢所有人的帮助。