您当前的位置:首页 > 计算机 > 编程开发 > 安卓(android)开发

Android SharedPreferences之登录界面并有记忆功能

时间:01-12来源:作者:点击数:
CDSY,CDSY.XYZ
登录界面并有记忆功能
在这里插入图片描述

demo是这个样子,下次进入的时候会保存上次的信息

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="please input your name"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_pw"
        android:hint="please input your paseword"
        android:layout_marginTop="10dp"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cb_rm"
            android:text="rememberMe"
            android:layout_centerVertical="true"

            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="login"
            android:layout_alignParentRight="true"
            android:onClick="login"
            />



    </RelativeLayout>

</LinearLayout>

保存文件的名字 config.xml

SharedPreferences sp = getSharedPreferences("config", Context.MODE_PRIVATE);

找key(标签)的时候没有 返回defvalue 空字符串

 String userName = sp.getString("userName", "");

获得读写操作

SharedPreferences.Editor editor = sp.edit();
下面附上完整代码:

Mainactivity

public class MainActivity extends AppCompatActivity {

    private EditText et_username;
    private EditText et_pw;
    private CheckBox cb_rm;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        String userName = sp.getString("userName", "");
        String pw = sp.getString("pw", "");
        boolean isChecked = sp.getBoolean("isChecked", false);
        showinfo(userName,pw,isChecked);


    }

    private void showinfo(String userName, String pw, boolean isChecked) {
        this.et_username.setText(userName);
        this.et_pw.setText(pw);
        this.cb_rm.setChecked(isChecked);
    }


    private void initView() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_pw = (EditText) findViewById(R.id.et_pw);
        cb_rm = (CheckBox) findViewById(R.id.cb_rm);
    }
    public void login(View v){
        String userName = et_username.getText().toString().trim();
        String pw = et_pw.getText().toString().trim();
        if (userName.isEmpty() || TextUtils.isEmpty(pw))/*判断空不空,两种方法第一个是string方法 第二个是textutils方法(静态)*/
        {
            //跳出窗口//
            MyToast.show(this,"username or pw cannot be empty");
            return;
        }
        boolean isChecked = cb_rm.isChecked();
        if ( ! cb_rm.isChecked())/*如果这个变量(方法)是is开头的 那么这个变量(方法)的返回值一定是布尔类型*/
        {

            MyToast.show(this,"please remember yourself");
            return;
        }
        boolean isSuccess = saveWithsp(userName, pw,isChecked);
        if (isSuccess)
            MyToast.show(this,"SUCCESS");
            else
                MyToast.show(this,"FALLED");
    }
    private boolean saveWithsp(String userName,String pw,boolean isChecked) {    /*把数据保存*/
        SharedPreferences.Editor editor = sp.edit();   
        editor.putString("userName", userName);
        editor.putString("pw", pw);
        editor.putBoolean("isChecked", isChecked);
        boolean isSuccess = editor.commit();
        return isSuccess;
    }

}
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门