1.xml布局
这里是一个模拟记住账号和密码操作
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity_Share"
android:orientation="vertical"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:id="@+id/end_1"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/end_2"
android:hint="密码"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定"
android:id="@+id/bnt1"
/>
</LinearLayout>
SharedPreferences类,它是一个轻量级的存储类。
存储信息到SharedPreferences的使用步骤
1.获取输入框的内容
String muser = end1.getText().toString();
String mpwd = end2.getText().toString();
2…获取SharePreference对象
//获取SharePreference对象(参数1:文件名,参数2:模式)
SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
3.获取Editor对象
SharedPreferences.Editor edr = share.edit();
4.存储信息
//存储信息(参数1“key”,参数2:值)
edr.putString("user",muser);
edr.putString("pwd",mpwd);
5.指定提交操作
edr.commit();
读取SharedPreferences的使用步骤
其实和存储的类型
1.获取SharePreference对象
//获取SharePreference对象(参数1:文件名,参数2:模式)
SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
2.根据key读取代码
//根据key读取代码(参数1:key,参数2:指定对应key不存在时,返回参数2的内容作为默认值)
String musers = share.getString("user","");
String mpwds = share.getString("pwd","");
3.显示在页面上
//这里先记得初始化EditText
end1.setText(musers);
end2.setText(mpwds);
完整代码
public class MainActivity_Share extends AppCompatActivity {
private EditText end1,end2;
private Button mbnt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__share);
initView();
}
private void initView() {
end1 = (EditText)findViewById(R.id.end_1);
end2 = (EditText) findViewById(R.id.end_2);
//SharePreference读取
//1.获取SharePreference对象(参数1:文件名,参数2:模式)
SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
//2.根据key读取代码(参数1:key,参数2:指定对应key不存在时,返回参数2的内容作为默认值)
String musers = share.getString("user","");
String mpwds = share.getString("pwd","");
//显示在页面上
end1.setText(musers);
end2.setText(mpwds);
mbnt = findViewById(R.id.bnt1);
mbnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//1.获取输入框的内容
String muser = end1.getText().toString();
String mpwd = end2.getText().toString();
//2.验证
if (muser.equals("admin") && mpwd.equals("admin")){
//2.1存储信息到SharePreference
//1.获取SharePreference对象(参数1:文件名,参数2:模式)
SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
//2.获取Editor对象
SharedPreferences.Editor edr = share.edit();
//3.存储信息(参数1“key”,参数2:值)
edr.putString("user",muser);
edr.putString("pwd",mpwd);
//清空edr.clear();
//4.指定提交操作
edr.commit();
Toast.makeText(MainActivity_Share.this,"登录成功",Toast.LENGTH_SHORT).show();
}else {
//2.2验证失败,提醒用户
Toast.makeText(MainActivity_Share.this,"账号或者密码错误!",Toast.LENGTH_SHORT).show();
}
}
});
}
}
成果
输入之后点击确定之后会记住当前账号和密码,返回在进入不会被清除.
Android萌新,有问题望大佬指出,谢谢