使用Intent与Bundle传递数据
1.使用Relativelayout相对布局
<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"
>
<TextView
android:id="@+id/tv_username"
style="@style/intent_login_textview"
android:text="@string/username"
/>
<EditText
android:id="@+id/et_username"
style="@style/intent_login_edittext"
android:layout_toRightOf="@id/tv_username"
android:hint="填写用户名" />
<TextView
android:id="@+id/tv_password"
style="@style/intent_login_textview"
android:layout_below="@id/tv_username"
android:text="@string/password"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/et_passsword"
style="@style/intent_login_edittext"
android:layout_below="@id/et_username"
android:layout_toRightOf="@id/tv_password"
android:hint="填写密码" />
<Button
android:id="@+id/btn_register"
android:layout_marginTop="100dp"
style="@style/intent_login_button"
android:layout_marginLeft="40dp"
android:text="注册"
/>
<Button
android: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">
- "android:layout_width"
>50dp
- "android:layout_height">wrap_content
- "textAllCaps">false
- "android:textColor">@color/black
- "android:textSize">15sp
- "android:textStyle">normal
style>
<style name="intent_login_edittext">
- "android:layout_width"
>match_parent
- "android:layout_height">wrap_content
- "textAllCaps">false
- "android:textColor">@color/black
- "android:textSize">10sp
- "android:textStyle">normal
style>
<style name="intent_login_button">
- "android:layout_width"
>wrap_content
- "android:layout_height">wrap_content
- "textAllCaps">false
- "android:textColor">@color/black
- "android:textSize">20sp
- "android:textStyle">normal
style>
public class LoginActivity extends AppCompatActivity {
private EditText et_password, et_username;
private Button btn_register, btn_login;
private String dq_name, dq_password;
@Override
protected 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 {
@Override
public 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;
}
}
}
}
<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"
>
<TextView
android:id="@+id/tv_username"
style="@style/intent_login_textview"
android:text="@string/username"
/>
<EditText
android:id="@+id/et_username"
style="@style/intent_login_edittext"
android:layout_toRightOf="@id/tv_username"
android:hint="填写用户名" />
<TextView
android:id="@+id/tv_password"
style="@style/intent_login_textview"
android:layout_below="@id/tv_username"
android:text="@string/password"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/et_passsword"
style="@style/intent_login_edittext"
android:layout_below="@id/et_username"
android:layout_toRightOf="@id/tv_password"
android:hint="填写密码" />
<TextView
android:id="@+id/tv_repassword"
style="@style/intent_login_textview"
android:layout_below="@id/tv_password"
android:text="@string/repassword"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/et_repasssword"
style="@style/intent_login_edittext"
android:layout_below="@id/et_passsword"
android:layout_toRightOf="@id/tv_repassword"
android:hint="再次填写密码" />
<Button
android:id="@+id/btn_confirm"
android:layout_marginTop="150dp"
style="@style/intent_login_button"
android:layout_marginLeft="40dp"
android:text="确认"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_marginTop="150dp"
android:layout_marginLeft="200dp"
style="@style/intent_login_button"
android:text="取消"
/>
RelativeLayout>
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;
@Override
protected 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();
}
@Override
public 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;
}
}