Andriod游戏开发系统控件-RadioButton

Andriod游戏开发系统控件-RadioButton

RadioButton是单选按钮,和CheckButton一样都基础Button。RadioButton一般都存在单选组(RadioGroup)中,当多个RadioButton存放在一个单选组中,只能选一个RadioButton;如果想实现RadioButton的多选,那就需要多个RadioGroup。

创建项目:RadioButtonProject

作者:wwj

日期:2012/5/11

功能:实现单选按钮

项目运行效果图:

修改布局文件:

=>>main.xml


<?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"
    >
 	<RadioGroup
    android:id="@+id/radGrp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/radB1"
        />
    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/radB2"
        />
    <RadioButton
        android:id="@+id/rb3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/radB3"
        />
    </RadioGroup>
    </LinearLayout>


修改velue文件

=>>string.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, RadioButtonActivity!</string>
    <string name="app_name">RadioButton</string>
    <string name="radB1">RadioButton1</string>
    <string name="radB2">RadioButton2</string>
    <string name="radB3">RadioButton3</string>

</resources>

修改Activity文件

=>>RadioButtonProject.java

package com.radioButton;

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

public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{
	private RadioButton rb1,rb2,rb3;
	private RadioGroup rg;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rb1 = (RadioButton) findViewById(R.id.rb1);
        rb2 = (RadioButton) findViewById(R.id.rb2);
        rb3 = (RadioButton) findViewById(R.id.rb3);
        rg = (RadioGroup) findViewById(R.id.radGrp);
        rg.setOnCheckedChangeListener(this);//将单选组绑定监听器
    }
    //重写监听器函数
    public void onCheckedChanged(RadioGroup group, int checkedId) {
		if(group==rg){
			String rbName = null;
			if(checkedId == rb1.getId()){
				rbName = rb1.getText().toString();
			} else if (checkedId == rb2.getId()){
				rbName = rb2.getText().toString();
			} else if(checkedId == rb3.getId()){
				rbName = rb3.getText().toString();
			}
			Toast.makeText(this,"选择了下标为“"+ rbName + "”的单选按钮",
					Toast.LENGTH_LONG).show();
			
		}
		
	}   
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值