Android的SharedPreferences存取String和List<String>类型(在Activity和Fragment内使用)
工程目录:MainActivitypackage com.example.demo_eight;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.widget.Button;impo
·
工程目录:
MainActivity
package com.example.demo_eight;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.demo_eight.R;
public class MainActivity extends AppCompatActivity {
private EditText et1, et2;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = findViewById(R.id.edit1);
et2 = findViewById(R.id.edit2);
sp = getPreferences(Activity.MODE_PRIVATE);
String username = sp.getString("username", ""); //第2参数表示按键名取不到值时,设置为空串
String password = sp.getString("password", "");
et1.setText(username);
et2.setText(password);
Button btn1=findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
Toast.makeText(getApplicationContext(), "输入的用户名为" + s1 + ",输入的密码为" + s2, Toast.LENGTH_LONG).show();
}
});
Button btn2=findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
et1.setText("");
et2.setText("");
Toast.makeText(getApplicationContext(), "取消登录", Toast.LENGTH_SHORT).show();
}
});
CheckBox cb = findViewById(R.id.cb);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if (arg1) {
Toast.makeText(MainActivity.this, "已保存登录信息", Toast.LENGTH_LONG).show();
sp.edit()
.putString("username", et1.getText().toString())
.putString("password", et2.getText().toString())
.commit();
} else {
Toast.makeText(MainActivity.this, "不保存登录信息", Toast.LENGTH_LONG).show();
sp.edit()
.putString("username", null)
.putString("password", null)
.commit();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:" />
<EditText
android:id="@+id/edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<CheckBox
android:id="@+id/cb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="记住我" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment内:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home ,container, false);
sp=getActivity().getSharedPreferences("Tips",Context.MODE_PRIVATE);
sp.edit()
.putString("tips","两只起死回生手,一颗安民济世心。")
.commit();
TextView tv2=(TextView) view.findViewById(R.id.tv_home_2);
tv2.setText(sp.getString("tips",""));
return view;
}
List类型:
存储(存放key为size,value为该数组大小值,其它为key为一个字符串加数组index,value为数组对应的值)
private List<String> outcomeitemList;`
SharedPreferences sp = this.getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putInt("size", outcomeitemList.size());
for (int i = 0; i < outcomeitemList.size(); i++) {
edit.putString("item" + i, outcomeitemList.get(i));
}
edit.commit();
取出:
SharedPreferences sp=this.getSharedPreferences("data",Context.MODE_PRIVATE);
int size=sp.getInt("size",0);
for(int i=0;i<size;i++) {
outcomeitemList.add ( sp.getString("item"+i,null));
}
更多推荐
所有评论(0)