这里有新鲜出炉的精品教程,程序狗速度看过来!
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。
这篇文章主要介绍了Android中使用SharedPreferences完成记住账号密码的功能,需要的朋友可以参考下
效果图:
记住密码后,再次登录就会出现账号密码,否则没有。
分析:
SharedPreferences可将数据存储到本地的配置文件中
SharedPreferences会记录CheckBox的状态,如果CheckBox被选,则将配置文件中记录的账号密码信息回馈给账号密码控件,否则清空。
SharedPreferences使用方法:
1、创建名为config的配置文件,并且私有
- private SharedPreferences config;
- config = getSharedPreferences("config", MODE_PRIVATE);
2、添加编辑器
- Editor edit = config.edit();
3、向内存中写入数据
- String username = et_username.getText().toString();
- String password = et_password.getText().toString();
- edit.putString("username", username).putString("password", password);
4、提交到本地
- edit.commit();
代码:
fry.Activity01
- package fry;
- import com.example.rememberUserAndPassword.R;
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Activity01 extends Activity {
- private Button btn_login;
- private TextView et_username;
- private TextView et_password;
- private CheckBox cb_choose;
- private SharedPreferences config;@Override protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity01);
- config = getSharedPreferences("config", MODE_PRIVATE);
- btn_login = (Button) findViewById(R.id.btn_login);
- et_username = (TextView) findViewById(R.id.et_username);
- et_password = (TextView) findViewById(R.id.et_password);
- cb_choose = (CheckBox) findViewById(R.id.cb_choose);
- //是否记住了密码,初始化为false
- boolean isCheck = config.getBoolean("isCheck", false);
- //Toast.makeText(this, isCheck+" ", Toast.LENGTH_SHORT).show();
- if (isCheck) {
- et_username.setText(config.getString("username", ""));
- et_password.setText(config.getString("password", ""));
- cb_choose.setChecked(isCheck);
- }
- }
- //权限要是public,要不然访问不到
- //因为在button控件中设置了android:onClick="onClick"
- public void onClick(View view) {
- Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
- Editor edit = config.edit();
- String username = et_username.getText().toString();
- String password = et_password.getText().toString();
- boolean isCheck = cb_choose.isChecked();
- //Toast.makeText(this, isCheck+" ", Toast.LENGTH_SHORT).show();
- //存储CheckBox的状态
- edit.putBoolean("isCheck", isCheck);
- if (isCheck) {
- edit.putString("username", username).putString("password", password);
- } else {
- edit.remove("username").remove("password");
- }
- //提交到本地
- edit.commit();
- }
- }
/记住账号和密码/res/layout/activity01.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/et_username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <EditText
- android:id="@+id/et_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10" >
- <requestFocus />
- </EditText>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- >
- <CheckBox
- android:id="@+id/cb_choose"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="记住密码"
- />
- </LinearLayout>
- <!-- android:onClick="onClick" 点击时去class中调用onClick方法,权限要为public -->
- <Button
- android:id="@+id/btn_login"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="登录"
- android:layout_gravity="center_horizontal"
- android:onClick="onClick"
- />
- </LinearLayout>
总结
以上所述是小编给大家介绍的Android中使用SharedPreferences完成记住账号密码的功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHPERZ网站的支持!
来源: http://www.phperz.com/article/17/0915/345686.html