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

本文介绍了一个改进的RadioButton使用案例,通过增加确认按钮而非立即反馈,实现了更合理的用户体验。文章详细展示了如何在Android应用中设置RadioButton及其相关逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    原来书上的例子不很好,一点击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选中其中一个,其余的都要处于不选中的状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值