android应用开发揭秘例example4-7笔记(RadioGroup和RadioButton的使用与改进)

    原来书上的例子不很好,一点击RadioButton的答案立即就判断结果。 更好的方式是选好之后,按个确认按钮来实现:
1. 先在main.xml中加个button:
android应用开发揭秘例example4-7笔记(RadioGroup和RadioButton的使用与改进) - huasoft - 快乐的机器猫 小桥加加网易分站
 
<?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"
    >
    <TextView 
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
  <RadioGroup
    android:id="@+id/RadioGroup01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_x="3px"
    android:layout_y="54px"
    >
    <RadioButton
      android:id="@+id/RadioButton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/RadioButton1"
    />
    <RadioButton
      android:id="@+id/RadioButton2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/RadioButton2"
    />
    <RadioButton
      android:id="@+id/RadioButton3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/RadioButton3"
    />
    <RadioButton
      android:id="@+id/RadioButton4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/RadioButton4"
    />
    </RadioGroup>
  <Button android:text="Commit" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>   
</LinearLayout>

2. 再改java程序:
package com.yarin.android.Examples_04_07;

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

public class Activity01 extends Activity
{
    /**
     * 创建TextView对象
     * 创建RadioGroup对象
     * 创建4个RadioButton对象
     */
    TextView        m_TextView;
    RadioGroup        m_RadioGroup;
    RadioButton        m_Radio1, m_Radio2, m_Radio3, m_Radio4;
    Button            mybutton1;
    int                youranswer=0;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /**
         * 获得TextView对象
         * 获得RadioGroup对象
         * 获得4个RadioButton对象
         */
        m_TextView = (TextView) findViewById(R.id.TextView01);
        m_RadioGroup = (RadioGroup) findViewById(R.id.RadioGroup01);
        m_Radio1 = (RadioButton) findViewById(R.id.RadioButton1);
        m_Radio2 = (RadioButton) findViewById(R.id.RadioButton2);
        m_Radio3 = (RadioButton) findViewById(R.id.RadioButton3);
        m_Radio4 = (RadioButton) findViewById(R.id.RadioButton4);
        mybutton1 = (Button)findViewById(R.id.button1);

        /* 设置事件监听  */
        m_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                // TODO Auto-generated method stub
                if(checkedId == m_Radio1.getId())
                    youranswer = 1;
                if(checkedId == m_Radio2.getId())
                    youranswer = 2;
                if(checkedId == m_Radio3.getId())
                    youranswer = 3;
                if(checkedId == m_Radio4.getId())
                    youranswer = 4;

//                if (checkedId == m_Radio2.getId())
//                {
//                    DisplayToast("正确答案:" + m_Radio2.getText() + ",恭喜你,回答正确!");
//                }
//                else
//                {
//                    DisplayToast("请注意,回答错误!");
//                }
            }
        });
       
       
        mybutton1.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(youranswer==2)
                    DisplayToast("你的答案正确,是第2项");

                else
                    DisplayToast("你的答案错误");
            }
           
        });
    }//onCreate end
   
    /* 显示Toast  */
    public void DisplayToast(String str)
    {
        Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
        //设置toast显示的位置
        toast.setGravity(Gravity.TOP, 0, 220);
        //显示该Toast
        toast.show();
    }
   
}

对于checkedId的判断, 是在 m_RadioGroup.setOnCheckedChangeListener 进行的, 注意是 m_RadioGroup这几个RadioButton都要属于RadioGroup。下一节说的多项选择CheckBox就不一样,每个CheckBox是独立的,没有一个统一的类似RadioGroup的东西来管理。

原因不言而喻, 引入RadioGroup是为了实现多选一的效果,即N个RadioButton选中其中一个,其余的都要处于不选中的状态。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,RadioButtonRadioGroup是常用的单选控件。RadioButton用于单个选项,而RadioGroup用于将多个RadioButton组合在一起,以便用户可以从中选择一个选项。 以下是一个使用RadioGroupRadioButton的示: 1. 在XML布局文件中添加RadioGroupRadioButton: ```xml <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" /> </RadioGroup> ``` 2. 在Java代码中获取RadioGroupRadioButton,并设置选中的选项: ```java RadioGroup radioGroup = findViewById(R.id.radioGroup); RadioButton radioButton1 = findViewById(R.id.radioButton1); RadioButton radioButton2 = findViewById(R.id.radioButton2); RadioButton radioButton3 = findViewById(R.id.radioButton3); // 设置默认选中的选项 radioButton1.setChecked(true); // 监听选项变化 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radioButton1: // 选中了Option 1 break; case R.id.radioButton2: // 选中了Option 2 break; case R.id.radioButton3: // 选中了Option 3 break; } } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值