您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Android 中使用Walker實現登錄記住密碼頁面功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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();// 關閉滑動菜單 } } }); } private void initMenuList() { /*設置數據源(圖片和文本信息)*/ 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); /*創建適配器*/ 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動畫結束事件。
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;//用于判斷是否是第一次運行,運行后變為false @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);//創建SharedPreferences對象 isFirst=pref.getBoolean("isFirstIn",true);//如果第一次運行,無isFirstIn值,自動獲取第二個參數為默認值 if(isFirst){//如果為true,進入if語句 intent=new Intent(WelcomeActivity.this,GiudeActivity.class); Editor editor=pref.edit(); editor.putBoolean("isFirstIn",false);//保存isFirstIn值為false editor.commit();//提交數據 }else{ intent=new Intent(WelcomeActivity.this,MainActivity.class);//如果為false,說明程序已經運行過,直接跳轉到主頁面 } 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頁面處理數據并保存。
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(); /*獲取數據*/ 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); } /*獲取數據*/ private void getData() { pref=getSharedPreferences("savaLogin",MODE_PRIVATE);//創建SharedPreferences對象 editor=pref.edit();//創建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); } } /*點擊事件保存數據*/ @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();//提交數據 }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); } }
上述內容就是怎么在Android 中使用Walker實現登錄記住密碼頁面功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。