登陆界面的UI设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#03A9F4"
android:orientation="vertical"
android:padding="30sp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--头像-->
<ImageView
android:id="@+id/one_image_0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/xionger" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--用户名和密码输入处使用嵌套布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/one_image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/user" />
<EditText
android:id="@+id/one_edit_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/one_image_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/password" />
<EditText
android:id="@+id/one_edit_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
</LinearLayout>
<!--相对布局-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/one_text_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="忘记密码?"
android:textAlignment="textEnd"
android:textColor="@color/white" />
</RelativeLayout>
<Button
android:id="@+id/one_button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/one_text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/one_button_1"
android:text="还不是会员?"
android:textColor="#756E6E"
android:textSize="15sp" />
<TextView
android:id="@+id/one_text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="免费注册"
android:textColor="@color/white"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/one_text_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/one_button_1"
android:text="-关联社交账号登录-"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="15sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/one_image_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/weixin" />
<ImageView
android:id="@+id/one_image_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/qq" />
<ImageView
android:id="@+id/one_image_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/weibo" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
编写Java代码,进行简单的用户账号和密码判断
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class OneActivity extends AppCompatActivity {
EditText oneEdit1;
EditText oneEdit2;
Button oneButton1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
//初始化UI界面
oneEdit1=findViewById(R.id.one_edit_1);
oneEdit2=findViewById(R.id.one_edit_2);
oneButton1=findViewById(R.id.one_button_1);
//设置监听器,监听按钮事件
oneButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置两个接收用户名和密码的参数
String user=oneEdit1.getText().toString().trim();
String password=oneEdit2.getText().toString().trim();
//判断用户名和密码是否为zhansan和ganfan
if ("zhansan".equals(user) && "ganfan".equals(password)){
Toast.makeText(OneActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(OneActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}