实验目的
- 掌握Activity的基本功能;
- 掌握preference的基本功能;
- 掌握断点的设置,调试程序;
实验内容
- 任务1:通过intent实现跳转,完成Activity之间的跳转;
- 任务2:intent数据的传递;
- 任务3:采用用preference实现随数据的存储;
- 任务4:掌握在虚拟机和真机环境下,对程序的调试;
实验要求
- 实现Android界面,并通过intent实现跳转,界面显示学生的姓名,学号,email
- 要求intent的实现传递姓名,学号,email等数据,到第二个activity
- 同时要求可以在虚拟机及手机上运行结果
- 采用preference实现对如上数据的存储,存储及读取
- 学会如何设置断点,并学会debug模式下,调试程序。
实验设计
- 两个Activity,一个做程序主入口,并对信息进行收集,另一个Activity做为前者的响应,对数据进行显示及保存。
public class MainActivity extends AppCompatActivity {
private EditText name;
private EditText number;
private RadioButton female;
private EditText email;
private Button btn;
private String sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
number = findViewById(R.id.number);
female = findViewById(R.id.female);
sex = female.isChecked()?"男":"女";
email = findViewById(R.id.email);
btn = findViewById(R.id.register);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Student stu = new Student(name.getText().toString(), number.getText().toString(), sex, email.getText().toString());
Bundle data = new Bundle();
data.putSerializable("student",stu);
Intent intent = new Intent(MainActivity.this,ResultActivity.class);
intent.putExtras(data);
//启动Activity
startActivity(intent);
}
});
}
}
public class ResultActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView name = findViewById(R.id.name);
TextView number = findViewById(R.id.number);
TextView sex = findViewById(R.id.sex);
TextView email = findViewById(R.id.email);
//获取本Activity的Intent
Intent intent = getIntent();
//获取传递的数据
Student stu = (Student)intent.getSerializableExtra("student");
name.setText("姓名:"+stu.getName());
number.setText("学号:"+stu.getNumber());
sex.setText("性别:"+stu.getSex());
email.setText("邮箱:"+stu.getEmail());
//拿到一个SharedPreference对象
SharedPreferences sp = getSharedPreferences("studentInfo", MODE_PRIVATE); //config为要生成的文件名
//拿到编辑器
SharedPreferences.Editor ed = sp.edit();
//写数据
ed.putString("name", name.getText().toString());
ed.putString("number",number.getText().toString());
ed.putString("sex",sex.getText().toString());
ed.putString("email",email.getText().toString());
//提交
ed.commit();
//显示
show();
}
public void show(){
//拿到一个SharedPreference对象
SharedPreferences sp = getSharedPreferences("studentInfo", MODE_PRIVATE);
//从SharedPreference里取数据
String stuName = sp.getString("name","");
String stuNumber = sp.getString("number","");
String stuSex = sp.getString("sex","");
String stuEmail = sp.getString("email","");
System.out.println("姓名:"+stuName);
System.out.println("学号:"+stuNumber);
System.out.println("性别:"+stuSex);
System.out.println("邮箱:"+stuEmail);
}
}
- 两个布局文件,对应于上述的两个Activity。
- activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入你的基本信息"
android:textSize="28sp"/>
<TableRow>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20sp"/>
<EditText android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="20sp"/>
</TableRow>
<TableRow>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="学号:"
android:textSize="20sp"/>
<EditText android:id="@+id/number"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="20sp"/>
</TableRow>
<TableRow>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="20sp"/>
<RadioGroup android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="男"
android:textSize="20sp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="女"
android:textSize="20sp"/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="邮箱:"
android:textSize="20sp"/>
<EditText android:id="@+id/email"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="20sp"/>
</TableRow>
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="20sp"/>
</TableLayout>
- result.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<TextView
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<TextView
android:id="@+id/sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"/>
<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"/>
</LinearLayout>
- 一个Student实体类,作为信息的封装:
public class Student implements Serializable {
private String name;
private String number;
private String sex;
private String email;
public Student(String name, String number, String sex, String email) {
this.name = name;
this.number = number;
this.sex = sex;
this.email = email;
}
//省略setter、getter方法及toString方法
}
实验结果
-
主界面
-
结果界面:
-
文件中显示: