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。這個(gè),“初始化用戶名管理員密碼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。這個(gè), “登陸成功!” , 吐司。LENGTH_SHORT).show(); //登陸成功后切換到MainActivity.class 意圖意圖= 新意圖(LoginActivity。此, 主要活動(dòng)。課程); //啟動(dòng) startActivity(intent); //登錄成功后銷毀登錄界面 finish(); } else { 吐司。makeText(LoginActivity。這個(gè),“登陸失?。 ?, 吐司。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 = “賬號(hào)/手機(jī)號(hào)/郵箱” 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 = “注冊(cè)” 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android Studio實(shí)現(xiàn)簡(jiǎn)易登錄界面制作
- Android?Studio實(shí)現(xiàn)登錄界面功能
- Android?studio?利用共享存儲(chǔ)進(jìn)行用戶的注冊(cè)和登錄驗(yàn)證功能
- Android Studio實(shí)現(xiàn)QQ的注冊(cè)登錄和好友列表跳轉(zhuǎn)
- Android Studio+Servlet+MySql實(shí)現(xiàn)登錄注冊(cè)
- Android Studio連接MySql實(shí)現(xiàn)登錄注冊(cè)(附源代碼)
- Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)
- Android Studio實(shí)現(xiàn)簡(jiǎn)單的QQ登錄界面的示例代碼
- Android Studio 通過登錄功能介紹SQLite數(shù)據(jù)庫(kù)的使用流程
- Android?studio實(shí)現(xiàn)app登錄界面
相關(guān)文章
Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式
這篇文章主要介紹了Android開發(fā)準(zhǔn)確獲取手機(jī)IP地址的兩種方式,需要的朋友可以參考下2020-03-03Android混合開發(fā)教程之WebView的使用方法總結(jié)
WebView是一個(gè)基于webkit引擎、展現(xiàn)web頁(yè)面的控件,下面這篇文章主要給大家介紹了關(guān)于Android混合開發(fā)教程之WebView的使用方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧2018-05-05Android TimerTask 的簡(jiǎn)單應(yīng)用及注意事項(xiàng)
這篇文章主要介紹了Android TimerTask 的簡(jiǎn)單應(yīng)用及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2017-06-06使用RecyclerView添加Header和Footer的方法
RecyclerView雖然作為L(zhǎng)istView的替代者有著較好的性能提升,但是ListView的一些常用功能卻沒有提供,比如我們平時(shí)會(huì)經(jīng)常用到的addHeaderView,addFooterView,既然RecyclerView沒有提供這個(gè)方法,我們應(yīng)該如何為列表添加頭部和底部呢,接下來通過本文給大家介紹2016-03-03Android中RecyclerView的item寬高問題詳解
RecyclerView出現(xiàn)已經(jīng)有一段時(shí)間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了關(guān)于Android中RecyclerView的item寬高問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08