阶段一:进行界面布局(如下图:)
具体代码如下:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:text="@string/inputname"
tools:context=".MainActivity" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inputsex" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inputsex"
android:orientation="horizontal"
android:id="@+id/sex" >
<RadioButton
android:text="男"
android:id="@+id/boy"
android:checked="true"/>
<RadioButton
android:text="女"
android:id="@+id/girl"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inputbirth" />
<DatePicker
android:id="@+id/birth"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inputhobby" />
<CheckBox
android:id="@+id/film"
android:text="看电影"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/basketball"
android:text="打篮球"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/game"
android:text="游戏"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/music"
android:text="听音乐"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"/>
</LinearLayout>
</ScrollView>
阶段二:编写Activity,查找组件并进行事件处理. 具体代码如下:
package com.lks.register;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
//定义组件
private EditText nameinput;
private RadioButton boy;
private RadioButton girl;
private DatePicker birth;
private CheckBox film;
private CheckBox basketball;
private CheckBox game;
private CheckBox music;
private Button clickButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找组件
nameinput=(EditText) findViewById(R.id.name);
boy=(RadioButton) findViewById(R.id.boy);
girl=(RadioButton) findViewById(R.id.girl);
birth=(DatePicker) findViewById(R.id.birth);
film=(CheckBox) findViewById(R.id.film);
basketball=(CheckBox) findViewById(R.id.basketball);
game=(CheckBox) findViewById(R.id.game);
music=(CheckBox) findViewById(R.id.music);
clickButton=(Button) findViewById(R.id.click);
clickButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name=nameinput.getText().toString();
String sex="";
if(boy.isChecked()){
sex="男";
}
if(girl.isChecked()){
sex="女";
}
String date=birth.getYear()+"年"+birth.getMonth()+"月"+birth.getDayOfMonth()+"日";
String hobby="";
if(film.isChecked()){
hobby=hobby+" "+film.getText();
}
if(basketball.isChecked()){
hobby=hobby+" "+basketball.getText();
}
if(game.isChecked()){
hobby=hobby+" "+game.getText();
}
if(music.isChecked()){
hobby=hobby+" "+music.getText();
}
Toast.makeText(MainActivity.this, "注册成功!\n您的姓名:"+name+"\n您的性别是:"+sex+"\n您的生日是:"+date+"\n您的爱好:"+hobby, Toast.LENGTH_LONG).show();
}
});
}
运行显示: