任务:
制作学生信息录入表:
开发步骤:
- 运用线性布局嵌套,按照模型制作activity_main.xml
- 添加组件TextView、EditText、单选框、复选框、按钮等
- 编辑MainActivity.java,制作各组件监听器
源代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学生信息录入:"
android:textSize="40sp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="30sp" />
<EditText
android:id="@+id/name"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textsex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="30sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:"
android:textSize="30sp" />
<EditText
android:id="@+id/age"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兴趣爱好:"
android:textSize="30sp" />
<CheckBox
android:id="@+id/basketball"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="篮球" />
<CheckBox
android:id="@+id/football"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="足球" />
<CheckBox
android:id="@+id/music"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音乐" />
</LinearLayout>
<Button
android:id="@+id/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定" />
<TextView
android:id="@+id/confirmed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="您的个人信息:" />
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="27dp"/>
</LinearLayout>
MainActivity.java
package com.example.excel;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity {
Button okBtn;
EditText editname,editage;
TextView showmesage;
RadioButton r1,r2;
CheckBox c1,c2,c3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editname = (EditText)findViewById(R.id.name);
editage = (EditText)findViewById(R.id.age);
okBtn = (Button)findViewById(R.id.confirm);
showmesage = (TextView)findViewById(R.id.message);
r1 = (RadioButton)findViewById(R.id.man);
r2 = (RadioButton)findViewById(R.id.woman);
c1 = (CheckBox)findViewById(R.id.basketball);
c2 = (CheckBox)findViewById(R.id.football);
c3 = (CheckBox)findViewById(R.id.music);
okBtn.setOnClickListener(new mClick());
}
class mClick implements OnClickListener{
public void onClick(View v){
CharSequence xingming = "",xingbie = "",nianling = "",aihao = "";
xingming = editname.getText();
if(r1.isChecked())
xingbie = r1.getText();
if(r2.isChecked())
xingbie = r2.getText();
nianling = editage.getText();
if(c1.isChecked())
aihao = aihao+" "+c1.getText();
if(c2.isChecked())
aihao = aihao+" "+c2.getText();
if(c3.isChecked())
aihao = aihao+" "+c3.getText();
showmesage.setText("姓名:"+xingming+"\n性别:"+xingbie+"\n年龄:"+nianling+"\n业余爱好:"+aihao);
}
}
}