Android Studio實現(xiàn)登錄功能案例講解
更新時間:2021年08月02日 10:16:39 作者:huangyixian2
這篇文章主要介紹了Android Studio實現(xiàn)登錄功能案例講解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
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();
//調用驗證是否第一次登錄方法
userUpdate();
//調用登錄驗證方法
loginVerify();
}
//判斷是否第一次登錄初始化用戶名密碼
public void userUpdate(){
//讀取文件內容
sharedPreferences = getSharedPreferences( “myfile”, MODE_PRIVATE);
//讀取數(shù)據(jù)
String susername = sharedPreferences .getString( “username”, “”);
String spwd = sharedPreferences .getString(“pwd”,“”);
//第一次進入程序讀取用戶名密碼為空,則寫入初始化數(shù)據(jù)并提示用戶
if(susername.equals(“”)&& spwd.equals(“”)){
//寫入數(shù)據(jù)
SharedPreferences.Editor editor = sharedPreferences .edit();
editor.putString(“username”,“admin”);
editor.putString(“pwd”,“123456”);
//提交數(shù)據(jù)
editor.commit();
吐司。makeText(LoginActivity。這個,“初始化用戶名管理員密碼123456” ,
吐司。LENGTH_LONG)。show();
}
}
//登錄驗證
public void loginVerify(){
//點擊登錄按鈕所發(fā)生的事件。(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);
//讀取數(shù)據(jù)
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 = “賬號/手機號/郵箱”
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 = “l(fā)eft | 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 = “l(fā)eft | 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 = “密碼”
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 = “l(fā)eft | center_vertical”
android :background = “@ drawable / mima” />
< 按鈕
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 = ” 登錄
“
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 = “忘記密碼”
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 = “l(fā)eft | center_vertical”
android :text = “注冊”
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 >
注意:一定要在AndroidManifest.xml里加activity?。。?!
到此這篇關于Android Studio實現(xiàn)登錄功能案例講解的文章就介紹到這了,更多相關Android Studio實現(xiàn)登錄內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- Android Studio實現(xiàn)簡易登錄界面制作
- Android?Studio實現(xiàn)登錄界面功能
- Android?studio?利用共享存儲進行用戶的注冊和登錄驗證功能
- Android Studio實現(xiàn)QQ的注冊登錄和好友列表跳轉
- Android Studio+Servlet+MySql實現(xiàn)登錄注冊
- Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實現(xiàn)
- Android Studio實現(xiàn)簡單的QQ登錄界面的示例代碼
- Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫的使用流程
- Android?studio實現(xiàn)app登錄界面
相關文章
Android混合開發(fā)教程之WebView的使用方法總結
WebView是一個基于webkit引擎、展現(xiàn)web頁面的控件,下面這篇文章主要給大家介紹了關于Android混合開發(fā)教程之WebView的使用方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧2018-05-05
使用RecyclerView添加Header和Footer的方法
RecyclerView雖然作為ListView的替代者有著較好的性能提升,但是ListView的一些常用功能卻沒有提供,比如我們平時會經常用到的addHeaderView,addFooterView,既然RecyclerView沒有提供這個方法,我們應該如何為列表添加頭部和底部呢,接下來通過本文給大家介紹2016-03-03
Android中RecyclerView的item寬高問題詳解
RecyclerView出現(xiàn)已經有一段時間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了關于Android中RecyclerView的item寬高問題的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08

