SharedPreferences是安卓里的一种存储方式,利用键值对来存储,比较小巧。
更多的使用的还是文件的IO方式来进行存储数据。
代码如下,submit按键测试SharedPreferences功能,存储的是程序启动的次数,write按键是写入文本文件,read按键是将文本内容写入标题栏。
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=".ListActivity22">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" 欢迎使用"
android:textSize="30dp"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/xin"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="文本:"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/tv" />
<EditText
android:id="@+id/ming"
android:layout_width="300dp"
android:layout_height="50dp"
app:layout_constraintLeft_toRightOf="@id/xin"
app:layout_constraintTop_toBottomOf="@id/tv" />
<TextView
android:id="@+id/xue"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="学号:"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/xin" />
<EditText
android:id="@+id/hao"
android:layout_width="300dp"
android:layout_height="50dp"
app:layout_constraintLeft_toRightOf="@id/xue"
app:layout_constraintTop_toBottomOf="@id/ming" />
<TextView
android:id="@+id/xinbie"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="性别:"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/xue" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintLeft_toRightOf="@id/xinbie"
app:layout_constraintTop_toBottomOf="@+id/hao">
<RadioButton
android:id="@+id/rb1"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Female"
app:layout_constraintLeft_toRightOf="@id/xinbie"
app:layout_constraintTop_toBottomOf="@+id/hao" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Male"
app:layout_constraintLeft_toRightOf="@id/rb2"
app:layout_constraintTop_toBottomOf="@+id/hao" />
</RadioGroup>
<TextView
android:id="@+id/mudi"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="使用目的:"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/xinbie" />
<CheckBox
android:id="@+id/xuexi"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="学习"
app:layout_constraintTop_toBottomOf="@id/mudi" />
<CheckBox
android:id="@+id/xiazai"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="下载资源"
app:layout_constraintLeft_toRightOf="@id/xuexi"
app:layout_constraintTop_toBottomOf="@id/mudi"
/>
<CheckBox
android:id="@+id/weiguan"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="上传资源"
app:layout_constraintLeft_toRightOf="@id/xiazai"
app:layout_constraintTop_toBottomOf="@id/mudi"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="看广告"
app:layout_constraintLeft_toRightOf="@id/weiguan"
app:layout_constraintTop_toBottomOf="@id/mudi" />
<TextView
android:id="@+id/nianji"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="年级:"
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@id/xuexi" />
<Spinner
android:id="@+id/sp"
android:layout_width="300dp"
android:layout_height="50dp"
android:entries="@array/mysp_arry"
app:layout_constraintLeft_toRightOf="@id/nianji"
app:layout_constraintTop_toBottomOf="@id/xuexi" />
<TextView
android:id="@+id/guanggao"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:text="广告投放"
android:textSize="10dp"
app:layout_constraintTop_toBottomOf="@id/nianji" />
<ListView
android:id="@+id/lv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/guanggao" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/lv3"
android:id="@+id/btn"
android:text="Submit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/btn"
app:layout_constraintTop_toBottomOf="@id/lv3"
android:id="@+id/btn1"
android:text="Write"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/btn1"
app:layout_constraintTop_toBottomOf="@id/lv3"
android:id="@+id/btn2"
android:text="Read"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Activity.java代码:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class ListActivity22 extends AppCompatActivity {
public String[] sp_array = {"广告位长期招租"};
public ListView lv3;
public RadioGroup rg;
public Button btn;
public String str;
public TextView tv;
public Button btn1;
public Button btn2;
public EditText ming;//获取xml里的editortext的内容,前面那个editortext就是叫ming
public final String file_name = "ttt.txt";
public SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list22);
lv3 = findViewById(R.id.lv3);
lv3.setAdapter(new MyAdapter(ListActivity22.this));
rg = findViewById(R.id.rg);//这一操作是将控件连接起来的操作。
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.rb1:str = "Female";break;
case R.id.rb2: str = "Male";break;
default: str = "nothing";break;
}
}
});
//shareedpreferce example
sp = getSharedPreferences("config",MODE_PRIVATE);
int c_count = sp.getInt("count",0);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("count",++c_count);
editor.commit();//记得提交
tv = findViewById(R.id.tv);
btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setText(str);
Toast.makeText(ListActivity22.this,String.valueOf(sp.getInt("count",0)),Toast.LENGTH_LONG).show();//toast弹出消息
}
});
ming = findViewById(R.id.ming);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
//下面写这些按钮的响应事件
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里面就是写响应事件
//write text from edittext to file
try {
FileOutputStream fos = openFileOutput(file_name,MODE_PRIVATE);
PrintStream ps = new PrintStream(fos);
ps.println(ming.getText());
ps.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// read text from file to textview
try {
FileInputStream fis = openFileInput(file_name);
byte[] buff = new byte[1024];
int hasRead = 0;
StringBuilder sb = new StringBuilder("");
while((hasRead=fis.read(buff))>0){
sb.append(new String(buff,0,hasRead));
}
fis.close();
tv.setText(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private class MyAdapter extends BaseAdapter {
Context context;
public MyAdapter(Context context) {
super();
this.context = context;
}
public MyAdapter() {
super();
this.context = context;
}
@Override
public int getCount() {
return sp_array.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
if(position%2==0){
v = LayoutInflater .from(context).inflate(R.layout.activity_calculator,null);
}else {
v = v = LayoutInflater.from(context).inflate(R.layout.activity_yueshu, null);
}
TextView tvv1 = v.findViewById(R.id.tvv1);
TextView tvv2 = v.findViewById(R.id.tvv2);
tvv1.setText(sp_array[position]);
tvv2.setText("张三");
return v;
}
}
}
来源:oschina
链接:https://my.oschina.net/u/4262068/blog/3234430