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();
			
		}
		
	}   
}


 


posted on 2012-05-11 15:57  1.曲待续  阅读(180)  评论(0编辑  收藏  举报

导航