Android Studio實(shí)現(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擴(kuò)展 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();
//調(diào)用驗(yàn)證是否第一次登錄方法
userUpdate();
//調(diào)用登錄驗(yàn)證方法
loginVerify();
}
//判斷是否第一次登錄初始化用戶名密碼
public void userUpdate(){
//讀取文件內(nèi)容
sharedPreferences = getSharedPreferences( “myfile”, MODE_PRIVATE);
//讀取數(shù)據(jù)
String susername = sharedPreferences .getString( “username”, “”);
String spwd = sharedPreferences .getString(“pwd”,“”);
//第一次進(jìn)入程序讀取用戶名密碼為空,則寫入初始化數(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();
}
}
//登錄驗(yàn)證
public void loginVerify(){
//點(diǎn)擊登錄按鈕所發(fā)生的事件。(1,驗(yàn)證合法2,驗(yàn)證不合法)
login .setOnClickListener( new OnClickListener(){
@Override
public void onClick(查看arg0){
//定義變量
String strUsername,strPassword;
//得到輸入框內(nèi)容
strUsername = username .getText()。toString();
strPassword = password .getText()。toString();
//讀取文件內(nèi)容得到用戶名密碼
sharedPreferences = getSharedPreferences( “myfile”, MODE_PRIVATE);
//讀取數(shù)據(jù)
String susername = sharedPreferences .getString( “username”, “”);
String spwd = sharedPreferences .getString(“pwd”,“”);
//登錄校驗(yàn)
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 = “賬號/手機(jī)號/郵箱”
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?。。?!
到此這篇關(guān)于Android Studio實(shí)現(xiàn)登錄功能案例講解的文章就介紹到這了,更多相關(guān)Android Studio實(shí)現(xiàn)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android Studio實(shí)現(xiàn)簡易登錄界面制作
- Android?Studio實(shí)現(xiàn)登錄界面功能
- Android?studio?利用共享存儲進(jìn)行用戶的注冊和登錄驗(yàn)證功能
- Android Studio實(shí)現(xiàn)QQ的注冊登錄和好友列表跳轉(zhuǎn)
- Android Studio+Servlet+MySql實(shí)現(xiàn)登錄注冊
- Android Studio連接MySql實(shí)現(xiàn)登錄注冊(附源代碼)
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實(shí)現(xiàn)
- Android Studio實(shí)現(xiàn)簡單的QQ登錄界面的示例代碼
- Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫的使用流程
- Android?studio實(shí)現(xiàn)app登錄界面
相關(guān)文章
Bootstrap 下拉菜單.dropdown的具體使用方法
這篇文章主要介紹了Bootstrap 下拉菜單.dropdown的具體使用方法,詳細(xì)講解下拉菜單的交互,有興趣的可以了解一下2017-10-10
OpenGL Shader實(shí)例分析(1)Wave效果
這篇文章主要為大家詳細(xì)介紹了OpenGL Shader實(shí)例分析第一篇,Wave效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android自動測試工具M(jìn)onkey的實(shí)現(xiàn)方法
本文主要介紹Android Monkey 實(shí)現(xiàn)方法,Monkey測試是一種為了測試軟件的穩(wěn)定性、健壯性的快速有效的方法,具有非常重要的參考價(jià)值,希望對小伙伴有所幫助2016-07-07
android Jsoup獲取網(wǎng)站內(nèi)容 android獲取新聞標(biāo)題實(shí)例
這篇文章主要為大家詳細(xì)介紹了android Jsoup獲取網(wǎng)站內(nèi)容,android獲取新聞標(biāo)題實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android 通過webservice上傳多張圖片到指定服務(wù)器詳解
這篇文章主要介紹了Android 通過webservice上傳多張圖片到指定服務(wù)器詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android 中ViewPager中使用WebView的注意事項(xiàng)
這篇文章主要介紹了Android 中ViewPager中使用WebView的注意事項(xiàng)的相關(guān)資料,希望通過本文大家在使用過程中遇到這樣的問題解決,需要的朋友可以參考下2017-09-09

