码迷,mamicode.com
首页 > 移动开发 > 详细

Android---18---RadioButton单选按钮

时间:2015-04-16 21:56:02      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:android开发   radiobutton   

MainActivity.java:



import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
/**
 * RadioButton
 * @author Caesar
 *
 */
public class MainActivity extends Activity implements OnClickListener{

	private Button button;
	private RadioGroup group;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		group = (RadioGroup) this.findViewById(R.id.sex);
		button = (Button) this.findViewById(R.id.button);
		button.setOnClickListener(this);
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//获得单选按钮组的选项个数
		int len = group.getChildCount();
		String msgsString= "";
		for (int i = 0; i < len;i++){
			RadioButton radioButton = (RadioButton) group.getChildAt(i);
			if (radioButton.isChecked()){
				msgsString = radioButton.getText().toString();
				break;
			}
		}
		Toast.makeText(MainActivity.this, msgsString, 1).show();
	}
}


activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_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="com.example.radiobuttondemo.MainActivity" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="性别" />

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

        <RadioButton
            android:id="@+id/boy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />

        <RadioButton
            android:id="@+id/girl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />
    </RadioGroup>

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="选择性别"/>

</LinearLayout>



Android---18---RadioButton单选按钮

标签:android开发   radiobutton   

原文地址:http://blog.csdn.net/u013476556/article/details/45080945

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!