Android中怎么實(shí)現(xiàn)記住密碼功能,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
目前創(chuàng)新互聯(lián)已為超過千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站運(yùn)營、企業(yè)網(wǎng)站設(shè)計(jì)、宕昌網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
LoginActivity.java
package com.wangdeqiang.www.chatwithrobot.BroadcastBestPractice;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import com.wangdeqiang.www.chatwithrobot.R;import static com.wangdeqiang.www.chatwithrobot.R.id.account;/** * @author */public class LoginActivity extends BaseActivity { private SharedPreferences pref; private SharedPreferences.Editor editor; private EditText accountEdit; private EditText passwordEdit; private Button login; private CheckBox rememberPass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); pref = PreferenceManager.getDefaultSharedPreferences(this); accountEdit = (EditText) findViewById(account); passwordEdit = (EditText) findViewById(R.id.password); rememberPass = (CheckBox) findViewById(R.id.remember_pass); login = (Button) findViewById(R.id.login); boolean isRemeber = pref.getBoolean("remember_password",false); if(isRemeber) { //將賬號(hào)和密碼都設(shè)置到文本框中 String account = pref.getString("account",""); String password = pref.getString("password",""); accountEdit.setText(account); passwordEdit.setText(password); rememberPass.setChecked(true); } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = pref.getString("account", "").toString(); String password = pref.getString("password", "").toString(); //如果賬號(hào)和密碼是admin和123456,就認(rèn)為登陸成功 if (account.equals("admin") && password.equals("123456")) { editor = pref.edit(); if (rememberPass.isChecked()) { editor.putBoolean("remember", false); editor.putString("account", account); editor.putString("password", password); } else { editor.apply(); } editor.commit(); Intent intent = new Intent(LoginActivity.this, Main3Activity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "密碼或者賬號(hào)錯(cuò)誤", Toast.LENGTH_SHORT).show(); } } }); } }
activity_login.xml
看完上述內(nèi)容,你們掌握Android中怎么實(shí)現(xiàn)記住密碼功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!