Android Walker登錄記住密碼頁面功能實現(xiàn)
本文實例為大家分享了Android Walker登錄記住密碼頁面的具體代碼,供大家參考,具體內(nèi)容如下
目標效果:

這一次修改的不多,添加了點擊用戶登錄的跳轉(zhuǎn),登錄頁面的記住密碼,和程序運行一次后,不進入導航頁面的功能。
1.MainActivity.java頁面修改了setOnItemClickListener的點擊事件,進行跳轉(zhuǎn)。
MainActivity.java頁面:
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()) {// 判斷滑動菜單是否已打開,如果未打開
slideMenu.openMenu(); // 打開滑動菜單
} else {
slideMenu.closeMenu();// 關(guān)閉滑動菜單
}
}
});
}
private void initMenuList() {
/*設置數(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);
/*實例列表*/
List<Item> items = new ArrayList<Item>();
/*向列表中添加圖片和對應的文本信息*/
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);
/*列表某一項的點擊事件*/
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頁面修改onAnimationEnd動畫結(jié)束事件。
WelcomeActivity.java頁面:
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ō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對象
isFirst=pref.getBoolean("isFirstIn",true);//如果第一次運行,無isFirstIn值,自動獲取第二個參數(shù)為默認值
if(isFirst){//如果為true,進入if語句
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,說明程序已經(jīng)運行過,直接跳轉(zhuǎn)到主頁面
}
startActivity(intent);
finish();
}
});
}
}
3.login_top.xml頁面添加多選框,用于選擇記住密碼。
login_top.xml頁面:
<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頁面處理數(shù)據(jù)并保存。
LoginActivity.java頁面:
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();
/*綁定點擊事件*/
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對象
editor=pref.edit();//創(chuàng)建SharedPreferences的編輯器
String userName=pref.getString("userName","");//獲取用戶名,沒有則用空代替
String userPass=pref.getString("userPass","");
if(userName.equals("")){//如果為空,代表前一次為選擇記住密碼,則這次顯示記住密碼多選框不打勾
cbSave.setChecked(false);
}else{
cbSave.setChecked(true);
etName.setText(userName);//將獲取到的值設置為text
etPass.setText(userPass);
}
}
/*點擊事件保存數(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{//若沒有選擇記住密碼
editor.remove("userPass");//刪除密碼
editor.commit();
}
Toast.makeText(LoginActivity.this,"登錄成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(LoginActivity.this,"用戶名或密碼錯誤",Toast.LENGTH_SHORT).show();
}
break;
case R.id.btRegister:
break;
}
}
private void bindClick() {
btLogin.setOnClickListener(this);
btRegister.setOnClickListener(this);
}
}
5.程序運行就顯示目標效果了。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用Scroll+Fragment仿京東分類效果
這篇文章主要為大家詳細介紹了Android使用Scroll+Fragment仿京東分類效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02
Android 中 退出多個activity的經(jīng)典方法
這篇文章主要介紹了Android 中 退出多個activity的經(jīng)典方法 的相關(guān)資料,本文給大家分享兩種方法,在這小編給大家推薦使用第一種方法,對此文感興趣的朋友可以參考下2016-09-09
Android開發(fā)筆記之:如何屏蔽Button setClickable與setEnabled
本篇文章是對在Android中,如何屏蔽Button setClickable與setEnabled的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android 進階實現(xiàn)性能優(yōu)化之OOM與Leakcanary詳解原理
LeakCanary 是大名鼎鼎的 square 公司開源的內(nèi)存泄漏檢測工具。目前上大部分App在開發(fā)測試階段都會接入此工具用于檢測潛在的內(nèi)存泄漏問題,做的好一點的可能會搭建一個服務器用于保存各個設備上的內(nèi)存泄漏問題再集中處理2021-11-11
react native打包apk文件安裝好之后進入應用閃退的解決方案
這篇文章主要介紹了react native打包apk文件安裝好之后進入應用閃退的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
Android利用AsyncTask異步類實現(xiàn)網(wǎng)頁內(nèi)容放大縮小
這篇文章主要為大家介紹了利用AsyncTask異步類實現(xiàn)網(wǎng)頁內(nèi)容放大縮小的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07

