設計用戶註冊頁面知識點:
CheckBox用於選取多個值時使用的組件,在佈局文件中使用<CheckBox>標籤標記,可以使用android:checked=“true”來設定默認選中值。
DatePicker組件可以輸入日期。範圍在1900-1-1 ~ 2100-12-31
RadioButton可以構建一組單選按鈕,一組互斥的單選按鈕必須在用一個RadioGroup中。
Toast:彈出提示框
ScrollView的使用:當一屏顯示不開時,自動加入滾動條,此時佈局文件中ScrollView處於頂級元素
運行效果圖:
註冊頁面的佈局文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/label_name" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/label_sex" />
<RadioGroup
android:id="@+id/sex"
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
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/label_birth" />
<DatePicker
android:id="@+id/birth"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/label_interest" />
<CheckBox
android:id="@+id/movie"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/movie" />
<CheckBox
android:id="@+id/basketball"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/basketball" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_submit" >
</Button>
</LinearLayout>
</ScrollView>
註冊成功時彈出註冊信息
public class RegisterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText nameText = (EditText) this.findViewById(R.id.name);
final RadioButton maleRadio = (RadioButton) this
.findViewById(R.id.male);
final RadioButton femaleRadio = (RadioButton) this
.findViewById(R.id.female);
final DatePicker birthDatePicker = (DatePicker) this
.findViewById(R.id.birth);
final CheckBox movieBox = (CheckBox) this.findViewById(R.id.movie);
final CheckBox basketballBox = (CheckBox) this
.findViewById(R.id.basketball);
Button submit = (Button) this.findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sex = "男";
if (maleRadio.isChecked()) {
sex = maleRadio.getText().toString();
}
if (femaleRadio.isChecked()) {
sex = femaleRadio.getText().toString();
}
String interest = "";
if (movieBox.isChecked()) {
interest = interest + movieBox.getText().toString();
}
if (basketballBox.isChecked()) {
interest = interest + basketballBox.getText().toString();
}
String birth = birthDatePicker.getYear() + "年"
+ (birthDatePicker.getMonth() + 1) + "月"
+ birthDatePicker.getDayOfMonth() + "日";
StringBuffer prompt = new StringBuffer();
prompt.append("註冊成功!\n您的姓名:")
.append(nameText.getText().toString())
.append("\n您的性別是:").append(sex).append("\n您的生日:")
.append(birth).append("\n您的愛好:").append(interest);
Toast.makeText(RegisterActivity.this, prompt, Toast.LENGTH_LONG)
.show();
}
});
}
}