Case explanation of Android Studio login function
- 2021-12-13 09:48:14
- OfStack
LoginActivity.java
包 com.hyx.example.mymap;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
公共类 LoginActivity扩展 Activity {
private EditText username = null ;
private EditText password = null ;
private Button login = null ;
private SharedPreferences sharedPreferences ;
//初始化控件
公共无效的init(){
用户名 =(的EditText)findViewById(R.id。 username_edit);
密码 =(的EditText)findViewById(R.id。 userpassword_edit);
login =(Button)findViewById( R.id.login_button);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
的setContentView(R.layout 登录); //显示自己的UI视图
// 初始化控件
init();
//调用验证是否第1次登录方法
userUpdate();
//调用登录验证方法
loginVerify();
}
//判断是否第1次登录初始化用户名密码
public void userUpdate(){
//读取文件内容
sharedPreferences = getSharedPreferences( “myfile”, MODE_PRIVATE);
//读取数据
String susername = sharedPreferences .getString( “username”, “”);
String spwd = sharedPreferences .getString(“pwd”,“”);
//第1次进入程序读取用户名密码为空,则写入初始化数据并提示用户
if(susername.equals(“”)&& spwd.equals(“”)){
//写入数据
SharedPreferences.Editor editor = sharedPreferences .edit();
editor.putString(“username”,“admin”);
editor.putString(“pwd”,“123456”);
//提交数据
editor.commit();
吐司。makeText(LoginActivity。这个,“初始化用户名管理员密码123456” ,
吐司。LENGTH_LONG)。show();
}
}
//登录验证
public void loginVerify(){
//点击登录按钮所发生的事件。(1,验证合法2,验证不合法)
login .setOnClickListener( new OnClickListener(){
@Override
public void onClick(查看arg0){
//定义变量
String strUsername,strPassword;
//得到输入框内容
strUsername = username .getText()。toString();
strPassword = password .getText()。toString();
//读取文件内容得到用户名密码
sharedPreferences = getSharedPreferences( “myfile”, MODE_PRIVATE);
//读取数据
String susername = sharedPreferences .getString( “username”, “”);
String spwd = sharedPreferences .getString(“pwd”,“”);
//登录校验
if(strUsername.equals(susername)&& strPassword.equals(spwd)){
//登录成功后提示
Toast。makeText(LoginActivity。这个, “登陆成功!” ,
吐司。LENGTH_SHORT).show();
//登陆成功后切换到MainActivity.class
意图意图= 新意图(LoginActivity。此,
主要活动。课程);
//启动
startActivity(intent);
//登录成功后销毁登录界面
finish();
} else {
吐司。makeText(LoginActivity。这个,“登陆失败!” ,
吐司。LENGTH_SHORT).show();
}
}
});
}
// @Override
// public boolean onCreateOptionsMenu(菜单菜单){
// getMenuInflater()。inflate(R.menu.main,menu);
//返回true;
//}
}
login.xml
< RelativeLayout xmlns : android = " http://schemas.android.com/apk/res/android "
xmlns : custom = " http://schemas.android.com/apk/res-auto "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : fitsSystemWindows = " true " >
< RelativeLayout
android : id = " @ + id / login_layout "
android : layout_width = " match_parent "
android : layout_height = " wrap_content "
android : layout_marginLeft = " 20dp "
android : layout_marginRight = " 20dp "
android : gravity = " center " >
< FrameLayout
android : id = " @ + id / username_layout "
android : layout_width = " fill_parent "
android : layout_height = " wrap_content "
android : layout_marginTop = " 55dp "
android : gravity = " center " >
< EditText
android : id = " @ + id / username_edit "
android : layout_width = " match_parent "
android : layout_height = " 55dp "
android : layout_marginTop = " 5dp "
android : hint = "Account number / Mobile phone number / Mailbox "
android : paddingRight = " 60dp "
android : maxLength = " 20 "
android : paddingLeft = " 55dp " >
</ EditText >
< ImageView
android : layout_width = " 22dp "
android : layout_height = " 21dp "
android : layout_marginStart = " 8dp "
android : layout_gravity = " left | center_vertical "
android : background = " @ drawable / zhanghao "
android : visibility = " visible " / >
< TextView
android : id = " @ + id / contry_sn "
android : layout_width = " 40dp "
android : layout_height = " 50dp "
android : layout_gravity = " left | center_vertical "
android : layout_marginTop = " 4dp "
android : gravity = " center "
android : text = " + 62 " android : textColor = " @
android : color / black "
android : textSize = " 18sp "
android : visibility = " invisible " />
</ FrameLayout >
< FrameLayout
android : id = " @ + id / usercode_layout "
android : layout_width = " fill_parent "
android : layout_height = " wrap_content "
android : layout_below = " @ id / username_layout "
android : layout_marginTop = " 6dp "
android : gravity = " center " >
< EditText
android : id = " @ + id / userpassword_edit "
android : layout_width = " match_parent "
android : layout_height = " 55dp "
android : hint = "Password"
android : inputType = " textPassword "
android : paddingRight = " 60dp "
android : maxLength = " 20 "
android : paddingLeft = " 55dp " >
</ EditText >
< ImageView
android : layout_width = " 24dp "
android : layout_height = " 22dp "
android : layout_marginStart = " 7dp "
android : layout_gravity = " left | center_vertical "
android : background = " @ drawable / mima " />
< Button
android : id = " @ + id / passwordeye "
android : layout_width = " 23dp "
android : layout_height = " 23dp "
android : background = " @ drawable / kejian "
android : layout_gravity = " right | center_vertical "
android : layout_marginRight = " 10dp "
/>
</ FrameLayout >
< Button
android : id = " @ + id / login_button "
android : layout_width = " wrap_content "
android : layout_height = " 45dp "
android : layout_below = " @ + id / usercode_layout "
android : layout_centerHorizontal = " true "
android : layout_marginTop = " 34dp "
android : background = " @ drawable / bluebutton "
android : textSize = " 18sp " android : text = " Login
"
android : textColor = "# fffffff " />
< Button
android : id = " @ + id / login_error "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : layout_alignRight = " @ id / login_button "
android : layout_below = " @ id / login_button "
android : text = "Forgot password"
android : background = "# 00000000 "
android : textSize = " 16sp " />
< Button
android : id = " @ + id / register "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : layout_alignLeft = " @ id / login_button "
android : layout_below = " @ id / login_button "
android : background = "# 00000000 "
android : gravity = " left | center_vertical "
android : text = "Registration"
android : textSize = " 16sp "
android : visibility = " visible " />
</ RelativeLayout >
< RelativeLayout
android : id = " @ + id / remember_layout000 "
android : layout_width = " wrap_content "
android : layout_height = " wrap_content "
android : layout_alignParentBottom = " true "
android : layout_marginBottom = " 1dp "
android : layout_marginLeft = " 20dp "
android : layout_marginRight = " 20dp " >
</ RelativeLayout >
</ RelativeLayout >
Note: 1 Be sure to add activity to AndroidManifest. xml! ! !