Android学习实践:10.单选按钮RadioButton

本文介绍如何在Android应用中创建单选按钮,并通过RadioGroup实现互斥选择功能。提供了RadioButtonActivity示例代码,展示了如何设置界面布局及监听单选按钮的状态变化。

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

在项目中新建一个Android Activity,名为RadioButtonActivity,layout文件名为activity_radiobutton,title设为"单选"

activity_radiobutton.xml内容如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="maxwoods.demo1.RadioButtonActivity" >

	<RadioGroup
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:id="@+id/radioGroup01">
    <RadioButton
        android:id="@+id/RadioButton01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="大" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="小" />
	</RadioGroup>
	<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/radioGroup02">
	    <RadioButton
	        android:id="@+id/RadioButton03"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:text="高" />
	    <RadioButton
	        android:id="@+id/radioButton4"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:text="低" />
	</RadioGroup>
	    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="TextView" />
</LinearLayout>

上面使用了RadioGroup为每对互斥的选项进行了分组,同一个RadioGroup中的RadioButton只有一个能为选中状态。

RadioButtonActivity.java代码如下:

package maxwoods.demo1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RadioButtonActivity extends AppCompatActivity implements OnCheckedChangeListener
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_radiobutton);
	    RadioGroup rg01=(RadioGroup)findViewById(R.id.radioGroup01);
	    rg01.setOnCheckedChangeListener(this);
	    RadioGroup rg02=(RadioGroup)findViewById(R.id.radioGroup02);
	    rg02.setOnCheckedChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.check_radio, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings)
		{
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void onCheckedChanged(RadioGroup group, int checkedId)
	{
		StringBuilder sb=new StringBuilder();
		TextView tv=(TextView)findViewById(R.id.textView1);
		if(group.getId()==R.id.radioGroup01)
		{
			sb.append("选择大小组\n");
		}
		else if(group.getId()==R.id.radioGroup02)
		{
			sb.append("选择高低组\n");
		}
		RadioButton rb=(RadioButton)findViewById(checkedId);
		sb.append(rb.getText());
		tv.setText(sb.toString());	
	}
}


需要注意的是,事件监听代码应用于RadioGroup,在onCheckedChanged对单选按钮的选择作出响应,参数group为触发事件的RadioGroup,checkedId为所选的RadioButton的Id。最终运行效果如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

螃蟹就是横着走

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值