我们先用一个点击注册,然后注册完之后,把用户名和密码都传送到登录页的例子。
写User这个类
package com.example.gogearsrunapplication.bean; import android.os.Build; import android.os.Parcel; import android.os.Parcelable; public class User implements Parcelable { private String id ; private String username ; private String password; private String userIntro; private Boolean angelMode; private String userIcon; public User(){ } protected User(Parcel in) { id = in.readString(); username = in.readString(); password = in.readString(); userIntro = in.readString(); byte tmpAngelMode = in.readByte(); angelMode = tmpAngelMode == 0 ? null : tmpAngelMode == 1; userIcon = in.readString(); } public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() { @Override public User createFromParcel(Parcel source) { User user = new User(); user.id = source.readString(); user.username = source.readString(); user.password = source.readString(); user.userIntro = source.readString(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { user.angelMode = source.readBoolean(); } user.userIcon = source.readString(); return user; } @Override public User[] newArray(int size) { return new User[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(id); dest.writeString(username); dest.writeString(password); dest.writeString(userIntro); dest.writeByte((byte) (angelMode == null ? 0 : angelMode ? 1 : 2)); dest.writeString(userIcon); } }
接下来写出这个Layout
<?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" android:gravity="center_horizontal" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/edt_user_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="18dp" android:hint="用户名"/> <EditText android:id="@+id/edt_user_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码" android:textColor="#000000"/> <CheckBox android:id="@+id/remember_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:textColor="#000000" android:textSize="15dp"/> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:textSize="18dp" android:textColor="#000000"/> <Button android:id="@+id/btn_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:textColor="#000000" android:textSize="18dp"/> </LinearLayout>
接下来是Register的Layout:
layout:
<?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:orientation="vertical" android:gravity="center" android:layout_height="match_parent" tools:context=".RegisterActivity"> <EditText android:id="@+id/userName_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" android:textSize="18dp" android:textColorHint="#707070" android:textColor="#000000"/> <EditText android:id="@+id/password_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="password" android:textSize="18dp" android:textColorHint="#707070" android:textColor="#000000"/> <Button android:id="@+id/submit_register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" android:textSize="18dp" android:textColor="#000000"/> </LinearLayout>
MainActivity.java 的主代码:
package com.example.restryapplication; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @BindView(R.id.edt_user_name) EditText edtUserName; @BindView(R.id.edt_user_password) EditText edtUserPassword; @BindView(R.id.remember_password) CheckBox rememberPassword; @BindView(R.id.btn_login) Button btnLogin; @BindView(R.id.btn_register) Button btnRegister; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick({R.id.btn_login, R.id.btn_register}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.btn_login: break; case R.id.btn_register: Intent intent = new Intent(this,RegisterActivity.class); startActivityForResult(intent,1); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode){ case 1: if(resultCode == RESULT_OK) { User user =data.getParcelableExtra("userName_password"); String userName = user.getUserName(); String password = user.getPassword(); edtUserName.setText(userName); edtUserPassword.setText(password); } break; default: } } }
RegisterActivity.java :
package com.example.restryapplication; import android.content.Intent; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class RegisterActivity extends AppCompatActivity { @BindView(R.id.userName_register) EditText userNameRegister; @BindView(R.id.password_register) EditText passwordRegister; @BindView(R.id.submit_register) Button submitRegister; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); ButterKnife.bind(this); } @OnClick(R.id.submit_register) public void onViewClicked() { String userName = userNameRegister.getText().toString(); String password = passwordRegister.getText().toString(); User user = new User(); user.setUserName(userName); user.setPassword(password); Intent intent = new Intent(RegisterActivity.this,MainActivity.class); intent.putExtra("userName_password",user); setResult(RESULT_OK,intent); finish(); } }
完成!
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦