安卓登录界面
问题
- 界面转换
- 密码显示
- 用户名密码比较
activity_main2.xml中的代码
<?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:background="@mipmap/beijing1"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_marginBottom="10dp"
android:src="@mipmap/denglu1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/yonghuming1" />
<EditText
android:id="@+id/zhanghao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="请输入用户名"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/mima1" />
<EditText
android:id="@+id/mima"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="请输入密码"
android:inputType="numberPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/xianshi"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="显示密码" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="忘记密码?" />
</LinearLayout>
<Button
android:id="@+id/denglu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="登录" />
</LinearLayout>
MainActivity2中的代码
注意
- 登录事件监听
比较用户密码
if方法
界面转换
//新建一个Intent(当前Activity, SecondActivity)=====显示Intent
intent = new Intent(MainActivity2.this, MainActivity3.class);
//启动Intent
startActivity(intent);
- 显示按钮事件监听
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
mme.setTransformationMethod(null);
} else
mme.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
代码如下(示例):
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity2 extends AppCompatActivity {
private Intent intent;
private EditText zhe;
private EditText mme;
private String zh;
private String mm;
private Button button;
private CheckBox button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
button = findViewById(R.id.denglu);
button2 = findViewById(R.id.xianshi);
zhe = findViewById(R.id.zhanghao);
mme = findViewById(R.id.mima);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dl(view);
}
});
button2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
mme.setTransformationMethod(null);
} else
mme.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
});
}
public void dl(View view) {
zh = zhe.getText().toString();
mm = mme.getText().toString();
if (zh.equals("admin") || mm.equals("123456")) {
//新建一个Intent(当前Activity, SecondActivity)=====显示Intent
intent = new Intent(MainActivity2.this, MainActivity3.class);
//启动Intent
startActivity(intent);
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
} else if (zh.equals("") || mm.equals("")) {
Toast.makeText(this, "姓名或密码不能为空", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "姓名或密码错误", Toast.LENGTH_SHORT).show();
}
}
}
MainActivity3中的代码
注意
多线程的设置和sleep方法
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity3 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
new Thread(() -> {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
}
}).start();
}
}