2020年11月20日 星期五

[Android]_ch7.1_shared preferences:存取偏好檔案的內容





[Android]_ch7.1_shared preferences:存取偏好檔案的內容
存取偏好設定檔案的說明如下:
呼叫Context的getSharedPreferences()取得SharedPreferences
getSharedPreferences settings=getSharedPreferences(String name, int mode)
呼叫SharedPreferences的edit()以取得SharedPreferences.Editor物件,方可編輯內容
settings.edit()
.putString(“key1", stringValue)
.putBoolean(“key2", booleanValue)
.putInt(“key3", intValue)
.commit();
呼叫SharedPreferences對應的getter方法取得對應資料
String fileName = settings.getString("key1", default_fileName);
呼叫SharedPreferences.Editor的remove()將指定資料移除
settings.edit()
.remove(key1)
.commit();
檔案儲存於 data/data/專案套件名稱/shared_prefs/prefSet.xml




package com.derek.ch21_sharepreference;

import android.R.integer;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;


public class MainActivity extends Activity {
Button btn1,btn2,btn3;
RadioButton rb1,rb2;
EditText et1,et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=(EditText)findViewById(R.id.editText1);
et2=(EditText)findViewById(R.id.editText2);
rb1=(RadioButton)findViewById(R.id.radioButton1);
rb2=(RadioButton)findViewById(R.id.radioButton2);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);

btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String fileName=et1.getText().toString();
boolean isAuto=false;
if(rb1.isChecked())
isAuto=true;
else
isAuto=false;
int sec=Integer.parseInt(et2.getText().toString());
SharedPreferences sp=getSharedPreferences("Gjun2019", MODE_PRIVATE);
Editor editor=sp.edit();
editor.putString("1", fileName);
editor.putBoolean("2", isAuto);
editor.putInt("3", sec);
editor.commit();
Toast.makeText(MainActivity.this, "1.儲存成功...", Toast.LENGTH_SHORT).show();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp=getSharedPreferences("Gjun2019", MODE_PRIVATE);
String fileName= sp.getString("1", "找不到檔案");
et1.setText(fileName);
boolean isAuto=sp.getBoolean("2", true);
if(isAuto)
rb1.setChecked(true);
else
rb2.setChecked(true);
int sec=sp.getInt("3", 120);
et2.setText(Integer.toString(sec));
Toast.makeText(MainActivity.this, "2.載入成功...", Toast.LENGTH_SHORT).show();
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
et1.setText("Gjun.txt");
rb2.setChecked(true);
et2.setText(Integer.toString(100));
Toast.makeText(MainActivity.this, "3.回復預設...", Toast.LENGTH_SHORT).show();
}
});
}

}



Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...