Android 实现记住用户名和密码的功能是通过SharedPreference 存储来实现的。创建一个复选按钮,通过按钮的否选取来进行事件处理。若按钮选中存储账号和密码的信息。若按钮没有选中,则清空账号和密码的信息。
结果演示:
源代码下载地址:
https://github.com/GXS1225/Android————-.git
分析
(1)判断是否输入了账号和密码
if(name.trim().equals("")){ Toast.makeText(this, "请您输入用户名!", Toast.LENGTH_SHORT).show(); return; } if(pswd.trim().equals("")){ Toast.makeText(this, "请您输入密码!", Toast.LENGTH_SHORT).show(); return; }
(2)在layout_main.xml定义一个 CheckBox,进行事件处理
//通过 boolean CheckBoxLogin = checkbox.isChecked(); //按钮被选中,下次进入时会显示账号和密码 if (CheckBoxLogin) { Editor editor = sp.edit(); editor.putString("uname", name); editor.putString("upswd", pswd); editor.putBoolean("auto", true); editor.commit(); } //按钮被选中,清空账号和密码,下次进入时会显示账号和密码 else { Editor editor = sp.edit(); editor.putString("uname", null); editor.putString("upswd", null); editor.putBoolean("auto", false); editor.commit(); }
(3) SharedPreference 的存储实现
//先定义 SharedPreferences sp = null; sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE); //对uname 和 upswd 的操作 if (sp.getBoolean("checkboxBoolean", false)) { uname.setText(sp.getString("uname", null)); upswd.setText(sp.getString("upswd", null)); checkboxButton.setChecked(true); }
(4)跳转到Content.java界面
//Intent跳转 Intent intent = new Intent(Welcome.this,Content.class); startActivity(intent); finish();
步骤:
先写一个登陆的界面: layout_main.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
Welcome.java
package com.gxs.login; import com.example.login.R; import com.gxs.listview.*; import android.os.Bundle; import android.preference.Preference; import android.app.Activity; import android.app.SearchManager.OnCancelListener; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.Log; import android.view.Menu; 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 Welcome extends Activity implements OnClickListener{ private EditText uname = null; private EditText upswd = null; private CheckBox checkboxButton = null; private Button login = null; SharedPreferences sp = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_main); sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE); init(); } public void init() { uname = (EditText) findViewById(R.id.user_input); upswd = (EditText) findViewById(R.id.pass_input); checkboxButton = (CheckBox) findViewById(R.id.checkBoxLogin); login = (Button) findViewById(R.id.new_user); if (sp.getBoolean("checkboxBoolean", false)) { uname.setText(sp.getString("uname", null)); upswd.setText(sp.getString("upswd", null)); checkboxButton.setChecked(true); } login.setOnClickListener(this); } @Override public void onClick(View v) { if (v == login){ String name = uname.getText().toString(); String pswd = upswd.getText().toString(); if(name.trim().equals("")){ Toast.makeText(this, "请您输入用户名!", Toast.LENGTH_SHORT).show(); return; } if(pswd.trim().equals("")){ Toast.makeText(this, "请您输入密码!", Toast.LENGTH_SHORT).show(); return; } boolean CheckBoxLogin = checkboxButton.isChecked(); if (CheckBoxLogin) { Editor editor = sp.edit(); editor.putString("uname", name); editor.putString("upswd", pswd); editor.putBoolean("checkboxBoolean", true); editor.commit(); } else { Editor editor = sp.edit(); editor.putString("uname", null); editor.putString("upswd", null); editor.putBoolean("checkboxBoolean", false); editor.commit(); } //Intent跳转 Intent intent=new Intent(Welcome.this,Content.class); startActivity(intent); finish(); } } }
Content.java
package com.gxs.listview; import java.util.List; import com.example.login.R; import com.gxs.*; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class Content extends Activity{ private ListView listview_fruits; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.content); } }
content.xml
更多内容请参考专题:Android密码使用教程
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。