android中实现记住密码功能
2015-06-02 09:53
736 查看
运行效果图
实例程序讲解
在打开登陆界面时,程序会自动填写上次您输入正确的用户名和密码。小编本次为大家提供的是用SharePreferences对象来实现此功能的。
activity_main.xml
我们定义了一个CheckBox控件,当用户但及登录按钮时如果勾选了此对话框,则可以记住用户输入的用户名和密码。
代码如下
有什么需要改进的地方,大家可以留言交流哈.
实例程序讲解
在打开登陆界面时,程序会自动填写上次您输入正确的用户名和密码。小编本次为大家提供的是用SharePreferences对象来实现此功能的。
activity_main.xml
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/loginbg" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <include layout="@layout/login_top"/> <include layout="@layout/login_bottom"/>" </LinearLayout></span>
<strong>login_bottom.xml</strong>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tvRegist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="21dp" android:layout_marginTop="18dp" android:text="@string/tvRegister" android:autoLink="all" android:textColorLink="#FF0066CC" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="24dp" android:src="@drawable/panda" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="28dp" android:src="@drawable/icon" /> </RelativeLayout><strong> </strong>login_top.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/btnbg_roundcorner" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <TextView android:id="@+id/tvUsername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/tvName" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/etUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvUsername" android:layout_below="@+id/tvUsername" android:background="@android:drawable/edit_text" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/tvPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etUsername" android:layout_below="@+id/etUsername" android:text="@string/tvPassword" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvPassword" android:layout_below="@+id/tvPassword" android:layout_marginTop="16dp" android:background="@android:drawable/edit_text" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/etPassword" android:layout_below="@+id/etPassword" android:layout_marginTop="20dp" android:background="#FF72CAE1" android:text="@string/btnLogin" /> <CheckBox android:id="@+id/cbRememberPass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etPassword" android:layout_alignTop="@+id/btnLogin" android:text="记住密码" /> </RelativeLayout></span>activity_success.xml
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SuccessActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/success" /> </RelativeLayout></span>
我们定义了一个CheckBox控件,当用户但及登录按钮时如果勾选了此对话框,则可以记住用户输入的用户名和密码。
代码如下
package com.example.minitwittersimulate; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText etUsername; private EditText etPassword; private CheckBox cbRememberPass; private Button btnLogin; protected SharedPreferences mSpSettings = null; private static final String PREFS_NAME = "NamePwd"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); findView(); setListener(); getData(); } private void findView(){ etUsername=(EditText) findViewById(R.id.etUsername); etPassword=(EditText) findViewById(R.id.etPassword); cbRememberPass=(CheckBox) findViewById(R.id.cbRememberPass); btnLogin=(Button) findViewById(R.id.btnLogin); }//绑定控件 private void setListener(){ btnLogin.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub if("wyl".equals(etUsername.getText().toString()) && "123".equals(etPassword.getText().toString())){ //判断用户名和密码 if(cbRememberPass.isChecked()){ //判断复选框是否选中 mSpSettings=getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); Editor edit=mSpSettings.edit(); edit.putBoolean("isKeeep", true); edit.putString("username", etUsername.getText().toString()); edit.putString("password", etPassword.getText().toString()); edit.commit(); //选中后记住密码 } else{ mSpSettings=getSharedPreferences(PREFS_NAME, MODE_PRIVATE); Editor edit=mSpSettings.edit(); edit.putBoolean("isKeeep", false); edit.putString("username", ""); edit.putString("password", ""); edit.commit(); //未选中没有记住密码 } Intent intent =new Intent(MainActivity.this,SuccessActivity.class); startActivity(intent); } else{ Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onResume(){ super.onResume(); getData(); }//在界面显示之前得到数据 private void getData() { // TODO Auto-generated method stub mSpSettings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); if(mSpSettings.getBoolean("isKeep",true)){ etUsername.setText(mSpSettings.getString("username", "")); etPassword.setText(mSpSettings.getString("password", "")); }//如果之前存储过,则显示在相应的文本里 else { etUsername.setText(""); etPassword.setText(""); }//如果之前没有显示过,则显示为空。 } }
package com.example.minitwittersimulate; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class SuccessActivity extends Activity { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) { <span style="white-space:pre"> </span>super.onCreate(savedInstanceState); <span style="white-space:pre"> </span>setContentView(R.layout.activity_success); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public boolean onCreateOptionsMenu(Menu menu) { <span style="white-space:pre"> </span>// Inflate the menu; this adds items to the action bar if it is present. <span style="white-space:pre"> </span>getMenuInflater().inflate(R.menu.success, menu); <span style="white-space:pre"> </span>return true; <span style="white-space:pre"> </span>} }
有什么需要改进的地方,大家可以留言交流哈.
相关文章推荐
- Android动态加载字节码
- Android中px、dp、dip、sp详解
- 城市定位,利用百度地图API定位到你当前的位置
- 木瓜妮子多媒体开发教程---第四天---Android下图像空间滤波
- 在线阅读android源代码
- android中TextView分段显示不同颜色,字体,时间
- VLC4Android源码分析
- Android VLC播放器二次开发3——音乐播放(歌曲列表+歌词同步滚动)
- Android开发教程--onConfigurationChanged不执行的解决办法
- Android VLC播放器二次开发2——CPU类型检查+界面初始化
- Android VLC播放器二次开发1——程序结构分析
- Android动态加载字节码
- vlc-android 获取MediaPlayerEncounteredError,MediaPlayerBuffering等各种事件的响应
- vlc android的编译及截图,录制视频等功能
- vlc-android源码编译过程记录
- Android Context 上下文 你必须知道的一切
- android handler消息机制
- Android 自定义控件 优雅实现元素间的分割线 (支持3.0以下)
- vlc-android 中调用用libvlcjni.so实现流媒体播放
- android 比较靠谱的图片压缩