使用键值对进行本地存储(分类存储、获取)

做法:

activity_two.xml

<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/ed_name1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入账号" />
    <EditText
        android:id="@+id/ed_pass1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码" />
    <CheckBox
        android:id="@+id/ed_cba"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住账号和密码"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="login1"
        android:text="登陆"/>
</LinearLayout>
ThreeActivity

package com.liyulei.memoryread;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class ThreeActivity extends AppCompatActivity {
    EditText ed_name1,ed_pass1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
    }
    //外存卡存储信息
    public void login1(View view ) {
        //拿到输入框的内容
        EditText ed_name1 = (EditText) findViewById(R.id.ed_name1);
        EditText ed_pass1 = (EditText) findViewById(R.id.ed_pass1);
        String name1 = ed_name1.getText().toString();
        String pass1 = ed_pass1.getText().toString();
        //判断checkbox是否选中
        CheckBox ed_cba = (CheckBox) findViewById(R.id.ed_cba);
        if (ed_cba.isChecked()) {
           //获取一个sharepreserce
            SharedPreferences sp = getSharedPreferences("info",MODE_PRIVATE);
            //获取编辑器
            SharedPreferences.Editor ed = sp.edit();
            ed.putString("name1",name1);
            ed.putString("pass1",pass1);
            //提交
            ed.commit();
            }
            Toast toast = Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG);
            toast.show();
        }
    //外存卡读取信息
    public void Account() {
        //获取sharePrefence
        SharedPreferences sharedPreferences = getSharedPreferences("info",MODE_PRIVATE);
        String name = sharedPreferences.getString("name1"," ");
        String pass = sharedPreferences.getString("pass1"," ");

        ed_name1.setText(name);
        ed_pass1.setText(pass);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值