标签:按钮 传值 textview creat 注册 reads short gis klist
在对组件进行注册时,只注册了EditText,却忘记了Button,导致程序一直闪退
输入信息
点击添加
成功跳转页面,并将数据传递
User.java
package com.example.register; import android.os.Parcel; import android.os.Parcelable; public class User implements Parcelable { private String name; private String age; private String sex; public User(String name, String age, String sex) { this.name = name; this.age = age; this.sex = sex; } public User() { } //读取 protected User(Parcel in) { name = in.readString(); age = in.readString(); sex = in.readString(); } //写入 @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeString(age); dest.writeString(sex); } @Override public int describeContents() { return 0; } public static final Creator<User> CREATOR = new Creator<User>() { @Override public User createFromParcel(Parcel in) { return new User(in); } @Override public User[] newArray(int size) { return new User[size]; } }; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
UserAddActivity.java
package com.example.register; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.Nullable; public class UserAddActivity extends Activity { private EditText mName; private EditText mAge; private EditText mSex; private Button mAddBtn; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_adduser); initView(); initListener(); } private void initListener() { mAddBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //添加按钮被点击了 handlerAdduser(); } }); } private void handlerAdduser() { String name = mName.getText().toString().trim(); if (TextUtils.isEmpty(name)) { Toast.makeText(this, "输入的姓名为空", Toast.LENGTH_SHORT).show(); return; } String age = mAge.getText().toString().trim(); if (TextUtils.isEmpty(age)) { Toast.makeText(this, "输入的年龄为空", Toast.LENGTH_SHORT).show(); return; } String sex = mSex.getText().toString().trim(); if (TextUtils.isEmpty(sex)) { Toast.makeText(this, "输入的性别为空", Toast.LENGTH_SHORT).show(); return; } //跳转并传值给AddShowActivity Intent intent = new Intent(this, AddShowActivity.class); User user = new User(); //将属性封装到user对象中 user.setName(name); user.setAge(age); user.setSex(sex); intent.putExtra("userKey",user); startActivity(intent); } private void initView() { mName=this.findViewById(R.id.add_name); mAge=this.findViewById(R.id.add_age); mSex=this.findViewById(R.id.add_sex); mAddBtn=this.findViewById(R.id.add_btn); } }
activity_adduser.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:layout_width="wrap_content" android:text="添加人员信息" android:textSize="30sp" android:layout_marginTop="20dp" android:layout_gravity="center" android:layout_height="wrap_content"> </TextView> <EditText android:id="@+id/add_name" android:layout_width="match_parent" android:text="请输入姓名" android:layout_gravity="center" android:layout_height="wrap_content"> </EditText> <EditText android:id="@+id/add_age" android:layout_width="match_parent" android:text="请输入年龄" android:layout_gravity="center" android:layout_height="wrap_content"> </EditText> <EditText android:id="@+id/add_sex" android:layout_width="match_parent" android:text="请输入性别" android:layout_gravity="center" android:layout_height="wrap_content"> </EditText> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_gravity="center" android:text="添加" android:layout_height="wrap_content"> </Button> </LinearLayout>
AddShowActivity.java
package com.example.register; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; import android.widget.TextView; import androidx.annotation.Nullable; public class AddShowActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_addshow); TextView nameshow =(TextView) this.findViewById(R.id.add_name_show); TextView ageshow =(TextView) this.findViewById(R.id.add_age_show); TextView sexshow =(TextView) this.findViewById(R.id.add_sex_show); //通过getIntent获取MainActivity传来的intent Intent intent = getIntent(); if (intent!=null) { User user =intent.getParcelableExtra("userKey"); if (user!=null) { nameshow.setText(user.getName()); ageshow.setText(user.getAge()); sexshow.setText(user.getSex()); } } } }
activity_addshow.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:layout_width="wrap_content" android:text="添加信息展示" android:textSize="30sp" android:layout_gravity="center" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:text="姓名" android:textSize="20sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/add_name_show" android:layout_width="wrap_content" android:textSize="25sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:text="年龄" android:textSize="20sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/add_age_show" android:layout_width="wrap_content" android:textSize="25sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:text="性别" android:textSize="20sp" android:layout_height="wrap_content"> </TextView> <TextView android:id="@+id/add_sex_show" android:layout_width="wrap_content" android:textSize="25sp" android:layout_height="wrap_content"> </TextView> </LinearLayout>
标签:按钮 传值 textview creat 注册 reads short gis klist
原文地址:https://www.cnblogs.com/yeyueweiliang/p/12275090.html