Android Walker登錄記住密碼頁(yè)面功能實(shí)現(xiàn)
本文實(shí)例為大家分享了Android Walker登錄記住密碼頁(yè)面的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:
這一次修改的不多,添加了點(diǎn)擊用戶登錄的跳轉(zhuǎn),登錄頁(yè)面的記住密碼,和程序運(yùn)行一次后,不進(jìn)入導(dǎo)航頁(yè)面的功能。
1.MainActivity.java頁(yè)面修改了setOnItemClickListener的點(diǎn)擊事件,進(jìn)行跳轉(zhuǎn)。
MainActivity.java頁(yè)面:
package com.example.login; import java.util.ArrayList; import java.util.List; import com.example.walkersimulate.util.SlideMenu; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private SlideMenu slideMenu; private ImageView ivSwitchSlideMenu; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initMenuList(); slideMenu = (SlideMenu) findViewById(R.id.slideMenu); ivSwitchSlideMenu = (ImageView) findViewById(R.id.switch_slidemenu); ivSwitchSlideMenu.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { if (slideMenu.isMainScreenShowing()) {// 判斷滑動(dòng)菜單是否已打開(kāi),如果未打開(kāi) slideMenu.openMenu(); // 打開(kāi)滑動(dòng)菜單 } else { slideMenu.closeMenu();// 關(guān)閉滑動(dòng)菜單 } } }); } private void initMenuList() { /*設(shè)置數(shù)據(jù)源(圖片和文本信息)*/ int[] icons = { R.drawable.icons_menu_login, R.drawable.icons_menu_sport, R.drawable.icons_menu_inform, R.drawable.icons_menu_history, R.drawable.icons_menu_weather, R.drawable.icons_menu_health, R.drawable.icons_menu_setting }; final String[] introductons = getResources().getStringArray( R.array.menulist); /*實(shí)例列表*/ List<Item> items = new ArrayList<Item>(); /*向列表中添加圖片和對(duì)應(yīng)的文本信息*/ for (int i = 0; i < icons.length; i++) { items.add(new Item(icons[i], introductons[i])); } ListView lvMenuList = (ListView) findViewById(R.id.lvMenuList); /*創(chuàng)建適配器*/ itemAdapter adapter = new itemAdapter(this, R.layout.menulist_item, items); /*配置適配器*/ lvMenuList.setAdapter(adapter); /*列表某一項(xiàng)的點(diǎn)擊事件*/ lvMenuList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { switch (position) { case 0: intent=new Intent(MainActivity.this,LoginActivity.class); startActivity(intent); break; default: break; } } }); } }
2.WelcomeActivity.java頁(yè)面修改onAnimationEnd動(dòng)畫結(jié)束事件。
WelcomeActivity.java頁(yè)面:
package com.example.login; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.widget.RelativeLayout; public class WelcomeActivity extends Activity { private Intent intent; private SharedPreferences pref; private boolean isFirst=true;//用于判斷是否是第一次運(yùn)行,運(yùn)行后變?yōu)閒alse @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); RelativeLayout layoutWelcome=(RelativeLayout) findViewById(R.id.layoutwelcome); AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f); alphaAnimation.setDuration(3000); layoutWelcome.startAnimation(alphaAnimation); alphaAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { judgeIntent(); } private void judgeIntent() { pref=getSharedPreferences("isFirst",MODE_PRIVATE);//創(chuàng)建SharedPreferences對(duì)象 isFirst=pref.getBoolean("isFirstIn",true);//如果第一次運(yùn)行,無(wú)isFirstIn值,自動(dòng)獲取第二個(gè)參數(shù)為默認(rèn)值 if(isFirst){//如果為true,進(jìn)入if語(yǔ)句 intent=new Intent(WelcomeActivity.this,GiudeActivity.class); Editor editor=pref.edit(); editor.putBoolean("isFirstIn",false);//保存isFirstIn值為false editor.commit();//提交數(shù)據(jù) }else{ intent=new Intent(WelcomeActivity.this,MainActivity.class);//如果為false,說(shuō)明程序已經(jīng)運(yùn)行過(guò),直接跳轉(zhuǎn)到主頁(yè)面 } startActivity(intent); finish(); } }); } }
3.login_top.xml頁(yè)面添加多選框,用于選擇記住密碼。
login_top.xml頁(yè)面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:background="@drawable/logintop_bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_top_margin" tools:context=".MainActivity" > <EditText android:id="@+id/etName" android:hint="@string/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/etaccount" android:drawablePadding="25dp" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/etPass" android:hint="@string/etPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etName" android:layout_below="@+id/etName" android:layout_marginTop="10dp" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/etpass" android:drawablePadding="10dp" android:ems="10" android:inputType="textPassword" /> <CheckBox android:id="@+id/cbSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="110dp" android:text="記住密碼"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etPass" android:layout_below="@+id/etPass" android:layout_marginTop="40dp" > <Button android:id="@+id/btLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btchange_bg" android:text="@string/btLogin" /> <Button android:id="@+id/btRegister" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btchange_bg" android:text="@string/btRegister" android:layout_marginLeft="5dp" /> </LinearLayout> </RelativeLayout>
4.LoginActivity.java頁(yè)面處理數(shù)據(jù)并保存。
LoginActivity.java頁(yè)面:
package com.example.login; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends Activity implements OnClickListener{ private EditText etName,etPass; private CheckBox cbSave; private Button btLogin,btRegister; private SharedPreferences pref; private Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); /*獲取控件id*/ getId(); /*獲取數(shù)據(jù)*/ getData(); /*綁定點(diǎn)擊事件*/ bindClick(); } /*獲取控件id*/ private void getId() { etName=(EditText) findViewById(R.id.etName); etPass=(EditText) findViewById(R.id.etPass); cbSave=(CheckBox) findViewById(R.id.cbSave); btLogin=(Button) findViewById(R.id.btLogin); btRegister=(Button) findViewById(R.id.btRegister); } /*獲取數(shù)據(jù)*/ private void getData() { pref=getSharedPreferences("savaLogin",MODE_PRIVATE);//創(chuàng)建SharedPreferences對(duì)象 editor=pref.edit();//創(chuàng)建SharedPreferences的編輯器 String userName=pref.getString("userName","");//獲取用戶名,沒(méi)有則用空代替 String userPass=pref.getString("userPass",""); if(userName.equals("")){//如果為空,代表前一次為選擇記住密碼,則這次顯示記住密碼多選框不打勾 cbSave.setChecked(false); }else{ cbSave.setChecked(true); etName.setText(userName);//將獲取到的值設(shè)置為text etPass.setText(userPass); } } /*點(diǎn)擊事件保存數(shù)據(jù)*/ @Override public void onClick(View view) { switch (view.getId()) { case R.id.btLogin: String userName=etName.getText().toString().trim();//獲取EditText中輸入的值,并去掉空格 String userPass=etPass.getText().toString().trim(); if("admin".equals(userName)&&"123456".equals(userPass)){ if(cbSave.isChecked()){ editor.putString("userName",userName); editor.putString("userPass",userPass); editor.commit();//提交數(shù)據(jù) }else{//若沒(méi)有選擇記住密碼 editor.remove("userPass");//刪除密碼 editor.commit(); } Toast.makeText(LoginActivity.this,"登錄成功",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(LoginActivity.this,"用戶名或密碼錯(cuò)誤",Toast.LENGTH_SHORT).show(); } break; case R.id.btRegister: break; } } private void bindClick() { btLogin.setOnClickListener(this); btRegister.setOnClickListener(this); } }
5.程序運(yùn)行就顯示目標(biāo)效果了。
源碼下載:Android Walker登錄記住密碼頁(yè)面
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用Scroll+Fragment仿京東分類效果
這篇文章主要為大家詳細(xì)介紹了Android使用Scroll+Fragment仿京東分類效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Android 中 退出多個(gè)activity的經(jīng)典方法
這篇文章主要介紹了Android 中 退出多個(gè)activity的經(jīng)典方法 的相關(guān)資料,本文給大家分享兩種方法,在這小編給大家推薦使用第一種方法,對(duì)此文感興趣的朋友可以參考下2016-09-09Android開(kāi)發(fā)筆記之:如何屏蔽Button setClickable與setEnabled
本篇文章是對(duì)在Android中,如何屏蔽Button setClickable與setEnabled的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android 進(jìn)階實(shí)現(xiàn)性能優(yōu)化之OOM與Leakcanary詳解原理
LeakCanary 是大名鼎鼎的 square 公司開(kāi)源的內(nèi)存泄漏檢測(cè)工具。目前上大部分App在開(kāi)發(fā)測(cè)試階段都會(huì)接入此工具用于檢測(cè)潛在的內(nèi)存泄漏問(wèn)題,做的好一點(diǎn)的可能會(huì)搭建一個(gè)服務(wù)器用于保存各個(gè)設(shè)備上的內(nèi)存泄漏問(wèn)題再集中處理2021-11-11react native打包apk文件安裝好之后進(jìn)入應(yīng)用閃退的解決方案
這篇文章主要介紹了react native打包apk文件安裝好之后進(jìn)入應(yīng)用閃退的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Android利用AsyncTask異步類實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容放大縮小
這篇文章主要為大家介紹了利用AsyncTask異步類實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容放大縮小的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07