标签:username password 界面 登录 安卓 et btn id dq
使用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"
>
<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">
<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;
@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;
}
}
}
}
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"
>
<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>
注册界面.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;
@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;
}
}
标签:username,password,界面,登录,安卓,et,btn,id,dq 来源: https://blog.csdn.net/qq_45402917/article/details/112391867
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。