Android基础-RadioButton用法

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

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

实现方法:

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

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

3、设置RadioGroup点击事件

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


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

布局文件代码:

    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" >


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


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


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


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


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


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






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


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

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、通过网络获取天气、网络查询火车票呀等等的小案例。希望大家可以从我的博客中学到想要的东西,也希望刚有学习兴趣的朋友可以坚持关注本人的博客,有相关的问题也可以提出来给大家解决或一起讨论。Android基础-RadioButton用法_第1张图片


你可能感兴趣的:(Android基础)