Android基础-RadioButton用法

在这里给大家介绍一下安卓RadioButton的用法,RadioButton是一个单选按钮,一般在选择男女的时候会用到RadioButton并且与RadioGroup一起使用。

下面就个大家介绍下单选按钮的用法,这里给大家做的一个小的案例希望大家会喜欢。

实现方法:

1、定义一个RadioGroup 在里面添加多个RadioButton

2、在activity定义并获取这些控件

3、设置RadioGroup点击事件

4、通过id进行一个判断并给出一个提示


-----------------------------------------------------------------------------------------------------------

布局文件代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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=".MainActivity" >


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" >


    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="安卓是基于什么操作系统" />


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


        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="window" />


        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="java" />


        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="linux" />
    </RadioGroup>

</LinearLayout>


</RelativeLayout>


注意:项目创建的时候用的是一个相对布局,我在里面用了一个线性布局,布局后面会给大家讲解的。


-------------------------------------------------------------------------------------------------

Activity代码:

package com.example.radiobutton;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;


public class MainActivity extends Activity {
//定义控件
private RadioGroup rg;
private RadioButton rb1,rb2,rb3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
rg=(RadioGroup) findViewById(R.id.rg);
rb1=(RadioButton) findViewById(R.id.rb1);
rb2=(RadioButton) findViewById(R.id.rb2);
rb3=(RadioButton) findViewById(R.id.rb3);
//设置RadioGroup点击事件
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
//进行判断如果选中第三个按钮那么提示回答正确否则提示回答错误
if (arg1==rb3.getId()){
Toast.makeText(getApplicationContext(), "恭喜您!回答正确!", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "您回答的答案错误!请选择其他选项", Toast.LENGTH_LONG).show();

}
}
});
}
}


------------------------------------------------------------------------------------------------

红色的表示关键的代码,因为是刚刚开始写自己的博客所以可能写的不是太好希望理解,如果大家觉得有什么地方要改进的地方希望可以提出来毕竟有交流才会有进步吗?开始的学习都是从一些简单的布局控件开始的,后面会给大家写出更多的博客,会有一些布局呀、多线程 、SQLite、通过网络获取天气、网络查询火车票呀等等的小案例。希望大家可以从我的博客中学到想要的东西,也希望刚有学习兴趣的朋友可以坚持关注本人的博客,有相关的问题也可以提出来给大家解决或一起讨论。


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值