登陆界面详细代码
1)创建“登录页面”程序
创建一个名为“登录页面”的布局文件login.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:layout_centerVertical="true"
android:background="@drawable/login">
<TextView
android:id="@+id/jiemian"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text= "登陆界面"
android:textSize="40sp"
android:textColor="#FFFFFF"
android:background="#FF8F03"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:layout_marginTop="20dp"
android:layout_below="@id/jiemian"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#FF8000"/>
<EditText
android:id="@+id/username"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_toRightOf="@id/name"
android:layout_below="@id/jiemian"
android:layout_marginTop="20dp"
android:hint="请输入昵称"
android:minLines="1"
android:textSize="15sp"
/>
<TextView
android:id="@+id/edit_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@id/name"
android:text="密 码:"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#FF8000"/>
<EditText
android:id="@+id/password"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_below="@id/name"
android:layout_marginTop="20dp"
android:hint="请输入密码"
android:inputType="textPassword"
android:minLines="1"
android:textSize="15sp"
android:layout_toRightOf="@id/edit_password"/>
<Button
android:id="@+id/submit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/password"
android:text="提交"
android:textSize="30sp"
android:textStyle="bold"/>
<Button
android:id="@+id/reg"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/submit"
android:layout_marginTop="40dp"
android:text="注册"
android:layout_below="@id/password"
android:textSize="30sp"
android:textStyle="bold"
android:onClick="Click"/>
</RelativeLayout>
创建“登录页面”的界面交互代码Login.java,该程序实现用户信息的输入与显示,并通过点击按钮实现界面的切换。关键代码如下:
package com.itheima.mylogin;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
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 androidx.appcompat.app.AppCompatActivity;
public class Login extends AppCompatActivity {
private EditText unameEt, upassEt;
private Button submit,register;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
unameEt = (EditText) findViewById(R.id.username);
upassEt = (EditText) findViewById(R.id.password);//register-findViewByld(R.id.register);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
//TODO Auto-generated method stub
String username = unameEt.getText().toString();
String password = upassEt.getText().toString();
SharedPreferences references = getSharedPreferences("account", Context.MODE_PRIVATE);
String name = references.getString("username", "");
String psw = references.getString("password", "");
Log.i("name", name );
if (username.isEmpty() | password.isEmpty()) {
String sb = "用户名和密码不能为空";
Toast.makeText(Login.this, sb.toString(), Toast.LENGTH_LONG).show();
} else if (name.equals(username) && psw.equals(password)) {
Intent intent1 = new Intent(Login.this, LoginOk.class);
startActivity(intent1);
} else {
String sb = "请输入正确的用户名和密码"