Activity界面基本实验

实验目的
  1. 掌握Activity的基本功能;
  2. 掌握preference的基本功能;
  3. 掌握断点的设置,调试程序;
实验内容
  1. 任务1:通过intent实现跳转,完成Activity之间的跳转;
  2. 任务2:intent数据的传递;
  3. 任务3:采用用preference实现随数据的存储;
  4. 任务4:掌握在虚拟机和真机环境下,对程序的调试;
实验要求
  1. 实现Android界面,并通过intent实现跳转,界面显示学生的姓名,学号,email
  2. 要求intent的实现传递姓名,学号,email等数据,到第二个activity
  3. 同时要求可以在虚拟机及手机上运行结果
  4. 采用preference实现对如上数据的存储,存储及读取
  5. 学会如何设置断点,并学会debug模式下,调试程序。
实验设计
  1. 两个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);
    }
}
  1. 两个布局文件,对应于上述的两个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>
  1. 一个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方法
}
实验结果
  1. 主界面
    在这里插入图片描述

  2. 结果界面:
    在这里插入图片描述

  3. 文件中显示:
    在这里插入图片描述

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值