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