nullnullRadioButton和CheckBox示例

最近笔者几篇文章介绍了改nullnull的文章. 关联文章的地址

    main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#00FF00"
        android:textSize="20dip" >
    </TextView>

    <RadioGroup
        android:id="@+id/genderRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/male" >
        </RadioButton>

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

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/eatCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/eat" >
        </CheckBox>

        <CheckBox
            android:id="@+id/sleepCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sleep" >
        </CheckBox>

        <CheckBox
            android:id="@+id/playBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/play" >
        </CheckBox>
    </LinearLayout>

    <Button
        android:id="@+id/confirmButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:text="@string/ok" >
    </Button>

</LinearLayout>
    每日一道理
站在历史的海岸漫溯那一道道历史沟渠:楚大夫沉吟泽畔,九死不悔;魏武帝扬鞭东指,壮心不已;陶渊明悠然南山,饮酒采菊……他们选择了永恒,纵然谄媚诬蔑视听,也不随其流扬其波,这是执著的选择;纵然马革裹尸,魂归狼烟,也要仰天长笑,这是豪壮的选择;纵然一身清苦,终日难饱,也愿怡然自乐,躬耕陇亩,这是高雅的选择。在一番选择中,帝王将相成其盖世伟业,贤士迁客成其千古文章。

    
 

    MainActivity如下:

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
	private RadioGroup mRadioGroup; 
	private CheckBox mEatCheckBox; 
	private CheckBox mSleepCheckBox; 
	private CheckBox mPlayCheckBox;
	private Button mConfirmButton;
	private TextView mResultTextView;
	private StringBuffer hobby;
	private String gender="";
	private String selectedResult="";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
	private void init(){
		mResultTextView = (TextView) findViewById(R.id.resultTextView);
		mRadioGroup = (RadioGroup) findViewById(R.id.genderRadioGroup);
		mRadioGroup.setOnCheckedChangeListener(new RadioButtonOnCheckedChangeListenerImpl());
		mEatCheckBox=(CheckBox) findViewById(R.id.eatCheckBox);
		mSleepCheckBox=(CheckBox) findViewById(R.id.sleepCheckBox);
		mPlayCheckBox=(CheckBox) findViewById(R.id.playBox);
		mConfirmButton = (Button) findViewById(R.id.confirmButton);
		mConfirmButton.setOnClickListener(new ButtonOnClickListenerImpl());
	}
	private class ButtonOnClickListenerImpl implements OnClickListener{
		@Override
		public void onClick(View view) {
			hobby=new StringBuffer();
			selectedResult=new String();
			for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
				RadioButton radioButton=(RadioButton) mRadioGroup.getChildAt(i);
				if (radioButton.isChecked()) {
					gender=radioButton.getText().toString();
					break;
				}
			}
			if (mEatCheckBox.isChecked()) {
				hobby.append(" ");
				hobby.append(mEatCheckBox.getText().toString());
			}
			if (mSleepCheckBox.isChecked()) {
				hobby.append(" ");
				hobby.append(mSleepCheckBox.getText().toString());
			}
			if (mPlayCheckBox.isChecked()) {
				hobby.append(" ");
				hobby.append(mPlayCheckBox.getText().toString());
			}
			selectedResult=gender+" "+hobby.toString();
			mResultTextView.setText(selectedResult);
		}
		
	}
	
	 private class RadioButtonOnCheckedChangeListenerImpl implements OnCheckedChangeListener{
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				RadioButton rb=(RadioButton)findViewById(group.getCheckedRadioButtonId());
				String currentSelected=rb.getText().toString();
				System.out.println(""+currentSelected);
			}}
}

    
 

 

文章结束给大家分享下程序员的一些笑话语录: 一个合格的程序员是不会写出 诸如 “摧毁地球” 这样的程序的,他们会写一个函数叫 “摧毁行星”而把地球当一个参数传进去。

posted @ 2013-05-21 20:44  xinyuyuanm  阅读(224)  评论(0编辑  收藏  举报