简单的小列子,要求用户名密码不能为空,这里默认了用户名为123456,密码为666666,用户点击登录后,若用户名密码全正确,显示“登陆成功”,用户名或密码未填写则显示“用户名或密码不能为空”,若用户名或密码错误则显示“用户名或密码错误”。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:paddingTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="286dp"
android:layout_weight="0"
app:srcCompat="@drawable/QQ" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
android:padding="20dp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="用户名" />
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="372dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
android:padding="20dp"
android:paddingTop="100dp">
<TextView
android:id="@+id/text2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密码" />
<EditText
android:id="@+id/edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:password="true" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="372dp"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.example.myapplication3;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.Console;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText edt1;
EditText edt2;
Button btn;
Toast mtoast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1=(EditText)findViewById(R.id.edit1);
edt2=(EditText)findViewById(R.id.edit2);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v){
if (edt1.getText().toString().equals("")||edt2.getText().toString().equals("")) {
showToast("用户名或密码不能为空!");
}else if(edt1.getText().toString().equals("123456")&&edt2.getText().toString().equals("666666")){
showToast("登录成功!!!");
}else{
showToast("用户名或密码错误!");
}
}
private void showToast(String msg){
mtoast=Toast.makeText(this,msg,Toast.LENGTH_SHORT);
mtoast.show();
}
}