一:public class HistoryBean implements Serializable { //新建一个类存放属性 private double forwardLimit; private double backwardLimit; private double forwardCriticalPoint;
public double getForwardLimit()
{
return forwardLimit;
}
public void setForwardLimit(double forwardLimit)
{
this.forwardLimit = forwardLimit;
}
public double getBackwardLimit() { return backwardLimit; }
public void setBackwardLimit(double backwardLimit) {
this.backwardLimit = backwardLimit;
}
public double getForwardCriticalPoint() {
return forwardCriticalPoint;
}
public void setForwardCriticalPoint(double forwardCriticalPoint) {
this.forwardCriticalPoint = forwardCriticalPoint;
}
}
二:public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {
private SharedPreferences pref;
HistoryBean historyBean = new HistoryBean();
public void handleMessage(@NonNull Message msg) { Gson gson = new Gson(); SharedPreferences.Editor historyData = getSharedPreferences(“historyData”,MODE_PRIVATE).edit();
SharedPreferences.Editor historyData = getSharedPreferences("historyData",MODE_PRIVATE).edit(); //调用SharedPreferences对象来获取SharePreferences对象
super.handleMessage(msg);
switch (msg.what) {
case 0:
edt_data.append(spMsg);
dataNum++;
if (dataNum > 300) {
edt_data.getText().clear();
dataNum = 0;
}
break;
case 1:
if (item == 3) {
if (page == 1) {
tv_spmad.setText("AD:" + b3);
if (type == 1) {
tv_spmqj.setText("设定值:" + b1);
}
if (type == 2) {
tv_spmdj.setText("设定值:" + b2);
historyBean.setBackwardLimit(b2); //存入
}
}
String jsonStr = gson.toJson(historyBean); //将对象转成Json
historyData.putString("fast",jsonStr); //将json存进去
historyData.commit(); //提交
} 三: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); hideBottomUIMenu(); getWindow().getDecorView().setOnSystemUiVisibilityChangeListener( new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { hideBottomUIMenu(); //Android隐藏虚拟菜单 } }); setContentView(R.layout.activity_main); pref = getSharedPreferences(“historyData”,MODE_PRIVATE); initView(); initData(); initEvent(); historyData(); }
四: private void historyData() { HistoryBean historyBean1 = null; String str = pref.getString(“fast”,""); //取出json if(!TextUtils.isEmpty(pref.getString(“fast”,""))){ historyBean1 = new Gson().fromJson(str,HistoryBean.class); //将json转为对象 his_too.setText(“历史值:” + historyBean1.getForwardLimit()); his_tfo.setText(“历史值:” + historyBean1.getForwardCriticalPoint()); his_tft.setText(“历史值:” + historyBean1.getReverseCriticalPoint()); his_tot.setText(“历史值:” + historyBean1.getBackwardLimit());
his_too2.setText("历史值" + historyBean1.getForwardLimit2());
his_tfo2.setText("历史值" + historyBean1.getForwardCriticalPoint2());
his_tft2.setText("历史值" + historyBean1.getReverseCriticalPoint2());
his_tot2.setText("历史值" + historyBean1.getBackwardLimit2());
}
}
|