一 布局:
activity_main.xml
<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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:orientation="vertical" >
<TextView
android:id="@+id/nameinput1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nameinput1"
tools:context=".MainActivity" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<TextView
android:id="@+id/nameinput2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nameinput2"
tools:context=".MainActivity" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioButton1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioButton2" />
</RadioGroup>
<TextView
android:id="@+id/nameinput3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nameinput3"
tools:context=".MainActivity" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="14dp" />
<TextView
android:id="@+id/nameinput4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nameinput4"
tools:context=".MainActivity" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="唱歌" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看电影" />
<CheckBox
android:id="@+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打篮球" />
<CheckBox
android:id="@+id/game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玩游戏" />
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clickbutton" />
</LinearLayout>
</ScrollView>
划线红字处就是在页面上加的滚动条,无论页面多大都能完全显示
string文件没做任何改变,只是简单的定义,这里就不再重复了。
二 方法activity
MainActivity.java
package w3.dyp.document;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity {
private EditText nameInput1;
private EditText nameInput2;
private EditText nameInput3;
private EditText nameInput4;
private RadioButton radioButton1;
private RadioButton radioButton2;
private Button clickbutton;
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox game;
private CheckBox basketball;
private DatePicker datePicker1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameInput1 = (EditText) this.findViewById(R.id.name);
clickbutton = (Button) this.findViewById(R.id.click);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
datePicker1 = (DatePicker) findViewById(R.id.datePicker1);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
game = (CheckBox) findViewById(R.id.game);
basketball = (CheckBox) findViewById(R.id.basketball);
clickbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String name = nameInput1.getText().toString();
String sex = "";
if (radioButton1.isChecked()) {
sex = "男";
}
if (radioButton2.isChecked()) {
sex = "女";
}
String date = datePicker1.getYear() + "年"
+ (datePicker1.getMonth()+1) + "月"
+ datePicker1.getDayOfMonth() + "日";
String hobby = "";
if (checkBox1.isChecked()) {
hobby = hobby + "," + checkBox1.getText();
}
if (basketball.isChecked()) {
hobby = hobby + "," + basketball.getText();
}
if (game.isChecked()) {
hobby = hobby + "," + game.getText();
}
if (checkBox2.isChecked()) {
hobby = hobby + "," + checkBox2.getText();
}
Toast.makeText(
MainActivity.this,
"注册成功!\n您的姓名:" + name + "\n您的性别是:" + sex + "\n您的生日是:"
+ date + "\n您的爱好:" + hobby, Toast.LENGTH_LONG)
.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}因为日期格式为英文的,从0-11,所以在月份上要加1才能正确显示。(如图划线部分)
1407

被折叠的 条评论
为什么被折叠?



