使用Intent与Bundle传递数据
登录界面login.xml
1.使用Relativelayout相对布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="20dp"android:paddingTop="20dp"><TextViewandroid:id="@+id/tv_username"style="@style/intent_login_textview"android:text="@string/username"/><EditTextandroid:id="@+id/et_username"style="@style/intent_login_edittext"android:layout_toRightOf="@id/tv_username"android:hint="填写用户名" /><TextViewandroid:id="@+id/tv_password"style="@style/intent_login_textview"android:layout_below="@id/tv_username"android:text="@string/password"android:layout_marginTop="20dp"/><EditTextandroid:id="@+id/et_passsword"style="@style/intent_login_edittext"android:layout_below="@id/et_username"android:layout_toRightOf="@id/tv_password"android:hint="填写密码" /><Buttonandroid:id="@+id/btn_register"android:layout_marginTop="100dp"style="@style/intent_login_button"android:layout_marginLeft="40dp"android:text="注册"/><Buttonandroid:id="@+id/btn_login"android:layout_marginTop="100dp"android:layout_marginLeft="200dp"style="@style/intent_login_button"android:text="登录"/>
</RelativeLayout>
2.样式,themes.xml中
<style name="intent_login_textview"><item name="android:layout_width">50dp</item><item name="android:layout_height">wrap_content</item><item name="textAllCaps">false</item><item name="android:textColor">@color/black</item><item name="android:textSize">15sp</item><item name="android:textStyle">normal</item></style><style name="intent_login_edittext"><item name="android:layout_width">match_parent</item><item name="android:layout_height">wrap_content</item><item name="textAllCaps">false</item><item name="android:textColor">@color/black</item><item name="android:textSize">10sp</item><item name="android:textStyle">normal</item></style><style name="intent_login_button"><item name="android:layout_width">wrap_content</item><item name="android:layout_height">wrap_content</item><item name="textAllCaps">false</item><item name="android:textColor">@color/black</item><item name="android:textSize">20sp</item><item name="android:textStyle">normal</item></style>
LoginActivity.java
public class LoginActivity extends AppCompatActivity {private EditText et_password, et_username;private Button btn_register, btn_login;private String dq_name, dq_password;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);//设置Activity显示的布局et_password = findViewById(R.id.et_passsword);//获取布局中的控件对象et_username = findViewById(R.id.et_username);btn_login = findViewById(R.id.btn_login);btn_register = findViewById(R.id.btn_register);Btnlitener btnlistener = new Btnlitener();btn_login.setOnClickListener(btnlistener);btn_register.setOnClickListener(btnlistener);}private class Btnlitener implements View.OnClickListener {@Overridepublic void onClick(View v) {Intent intent;switch (v.getId()) {case R.id.btn_register:String username = et_username.getText().toString();String password = et_password.getText().toString();intent = new Intent(LoginActivity.this, RegisterActivity.class);intent.putExtra("username", username);intent.putExtra("password", password);startActivity(intent);break;case R.id.btn_login:String username1 = et_username.getText().toString();String password1 = et_password.getText().toString();intent = getIntent();Bundle bundle = intent.getExtras();dq_name = bundle.getString("dq_name");dq_password = bundle.getString("dq_password");if (username1.equals("")||password1.equals("")){Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();}else {if (username1.equals(dq_name) && (password1.equals(dq_password))) {startActivity(new Intent(LoginActivity.this, NextActivity.class));} else {Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();}}break;}}}}
register注册.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="20dp"android:paddingTop="20dp"><TextViewandroid:id="@+id/tv_username"style="@style/intent_login_textview"android:text="@string/username"/><EditTextandroid:id="@+id/et_username"style="@style/intent_login_edittext"android:layout_toRightOf="@id/tv_username"android:hint="填写用户名" /><TextViewandroid:id="@+id/tv_password"style="@style/intent_login_textview"android:layout_below="@id/tv_username"android:text="@string/password"android:layout_marginTop="20dp"/><EditTextandroid:id="@+id/et_passsword"style="@style/intent_login_edittext"android:layout_below="@id/et_username"android:layout_toRightOf="@id/tv_password"android:hint="填写密码" /><TextViewandroid:id="@+id/tv_repassword"style="@style/intent_login_textview"android:layout_below="@id/tv_password"android:text="@string/repassword"android:layout_marginTop="20dp"/><EditTextandroid:id="@+id/et_repasssword"style="@style/intent_login_edittext"android:layout_below="@id/et_passsword"android:layout_toRightOf="@id/tv_repassword"android:hint="再次填写密码" /><Buttonandroid:id="@+id/btn_confirm"android:layout_marginTop="150dp"style="@style/intent_login_button"android:layout_marginLeft="40dp"android:text="确认"/><Buttonandroid:id="@+id/btn_cancel"android:layout_marginTop="150dp"android:layout_marginLeft="200dp"style="@style/intent_login_button"android:text="取消"/>
</RelativeLayout>
注册界面.java
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {private EditText et_username, et_password, et_repassword;private Button btn_cancel, btn_confirm;private int count;private long t;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register);et_username = findViewById(R.id.et_username);et_password = findViewById(R.id.et_passsword);et_repassword = findViewById(R.id.et_repasssword);btn_cancel = findViewById(R.id.btn_cancel);btn_confirm = findViewById(R.id.btn_confirm);btn_cancel.setOnClickListener(this::onClick);btn_confirm.setOnClickListener(this::onClick);Intent intent = getIntent();String username = intent.getStringExtra("username");String password = intent.getStringExtra("password");et_username.setText(username);et_password.setText(password);et_repassword.setText(password);t = System.currentTimeMillis();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_cancel:et_repassword.setText("");et_password.setText("");et_username.setText("");long t1 = System.currentTimeMillis();if (t1 - t < 2000) {startActivity(new Intent(RegisterActivity.this, LoginActivity.class));}else {t = System.currentTimeMillis();Toast.makeText(RegisterActivity.this, "连续按两次回到登录界面", Toast.LENGTH_LONG).show();}break;case R.id.btn_confirm:if ((!"".equals(et_username.getText().toString())) && (!"".equals(et_password.getText().toString())) && (!"".equals(et_repassword.getText().toString()))) {if (et_password.getText().toString().equals(et_repassword.getText().toString())) {Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);String dq_name = et_username.getText().toString();String dq_password = et_password.getText().toString();Bundle bundle = new Bundle();bundle.putString("dq_name", dq_name);bundle.putString("dq_password", dq_password);intent.putExtras(bundle);startActivity(intent);} else {Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_LONG).show();}} else {Toast.makeText(RegisterActivity.this, "不能为空", Toast.LENGTH_LONG).show();}break;}}