Activity与Intent的使用

项目案例:学生注册界面,完成学生注册功能,并将学生消息传递到另一个Activity里。

目录

activity_main.xml文件

MainActivity.java文件

Student.java文件

activity_show.xml文件

MyAdapter.java文件

layout_item.xml文件

ShowActivity.java文件


项目代码:

activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="学生信息注册"
        android:textSize="35sp"
        android:textColor="@color/purple_500"
        android:textStyle="bold"
        android:gravity="center_horizontal"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="20sp"
            android:textColor="#000000"/>
        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="学号:"
            android:textSize="20sp"
            android:textColor="#000000"/>
        <EditText
            android:id="@+id/et_sNum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年级:"
            android:textSize="20sp"
            android:textColor="#000000"/>
        <EditText
            android:id="@+id/et_grade"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="班级:"
            android:textSize="20sp"
            android:textColor="#000000"/>
        <EditText
            android:id="@+id/et_class"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别:"
            android:layout_gravity="center_vertical"
            android:textSize="20sp"
            android:textColor="#000000"/>
        <RadioGroup
            android:id="@+id/rg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <RadioButton
                android:id="@+id/rb_nan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:textSize="20sp"/>
            <RadioButton
                android:id="@+id/rb_nv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"
                android:textSize="20sp"/>
        </RadioGroup>


    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:onClick="register"
        android:layout_gravity="center_horizontal"/>


</LinearLayout>

MainActivity.java文件

package com.example.exp_4;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
    private EditText et_name;
    private EditText et_sNum;
    private EditText et_grade;
    private EditText et_class;
    private RadioGroup radioGroup;
    private String sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText) findViewById(R.id.et_name);
        et_sNum = (EditText) findViewById(R.id.et_sNum);
        et_grade = (EditText) findViewById(R.id.et_grade);
        et_class = (EditText) findViewById(R.id.et_class);
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.rb_nan){
                    sex = "男";
                }else if(i==R.id.rb_nv){
                    sex = "女";
                }
            }
        });
    }

    public void register(View view) {
        String name = et_name.getText().toString().trim();
        String sNum = et_sNum.getText().toString().trim();
        String grade = et_grade.getText().toString().trim();
        String sClass = et_class.getText().toString().trim();
        Student student = new Student();
        student.setName(name);
        student.setsNum(sNum);
        student.setsClass(sClass);
        student.setSex(sex);

        Intent intent = new Intent(this,ShowActivity.class);
        intent.putExtra("student",student);
        startActivity(intent);

    }
}

Student.java文件

package com.example.exp_4;

import java.io.Serializable;

public class Student implements Serializable {
    private String name;
    private String sNum;
    private String grade;
    private String sClass;
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getsNum() {
        return sNum;
    }

    public void setsNum(String sNum) {
        this.sNum = sNum;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getsClass() {
        return sClass;
    }

    public void setsClass(String sClass) {
        this.sClass = sClass;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

activity_show.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="姓名"
            android:gravity="center_horizontal"
            android:textSize="30sp"
            android:textColor="@color/black"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="学号"
            android:gravity="center_horizontal"
            android:textSize="30sp"
            android:textColor="@color/black"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="年级"
            android:gravity="center_horizontal"
            android:textSize="30sp"
            android:textColor="@color/black"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="班级"
            android:gravity="center_horizontal"
            android:textSize="30sp"
            android:textColor="@color/black"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="性别"
            android:gravity="center_horizontal"
            android:textSize="30sp"
            android:textColor="@color/black"/>

    </LinearLayout>
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>


</LinearLayout>

MyAdapter.java文件

package com.example.exp_4;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
    private Student student;
    private Context context;

    public MyAdapter(Context context, Student student) {
        this.student = student;
        this.context = context;
    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public Object getItem(int i) {
        return student;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;

        //优化
        if (view == null){
            viewHolder = new ViewHolder();
            //加载视图,inflate解析器方法
            view = View.inflate(context,R.layout.layout_item,null);
            viewHolder.tvName= view.findViewById(R.id.tv_name);
            viewHolder.tvSNum = view.findViewById(R.id.tv_sNum);
            viewHolder.tvGrade = view.findViewById(R.id.tv_grade);
            viewHolder.tvClass= view.findViewById(R.id.tv_class);
            viewHolder.tvSex = view.findViewById(R.id.tv_sex);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }
        //TextView显示
        viewHolder.tvName.setText(student.getName());
        viewHolder.tvSNum.setText(student.getsNum());
        viewHolder.tvGrade.setText(student.getName());
        viewHolder.tvClass.setText(student.getsClass());
        viewHolder.tvSex.setText(student.getSex());
        return view;
    }
    //避免每次查找类
    class ViewHolder{
        TextView tvName;
        TextView tvSNum;
        TextView tvGrade;
        TextView tvClass;
        TextView tvSex;
    }
}

layout_item.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="姓名"
            android:gravity="center_horizontal"
            android:textSize="20sp"
           />
        <TextView
            android:id="@+id/tv_sNum"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="学号"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            />
        <TextView
            android:id="@+id/tv_grade"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="年级"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            />
        <TextView
            android:id="@+id/tv_class"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="班级"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            />
        <TextView
            android:id="@+id/tv_sex"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="性别"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            />

    </LinearLayout>

</LinearLayout>

ShowActivity.java文件

package com.example.exp_4;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

public class ShowActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        Intent intent = getIntent();
        Student student = (Student) intent.getSerializableExtra("student");
        ListView listView = (ListView) findViewById(R.id.lv);
        MyAdapter myAdapter = new MyAdapter(this,student);
        listView.setAdapter(myAdapter);

    }
}

  

 

 

 

 

 

 

 

 

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值