iT邦幫忙

0

每日一篇學習筆記 直到我做完專題 :( [Day23]

  • 分享至 

  • xImage
  •  

今天開始步入存儲數據的部分

首先學習sharepreferences的部分

先寫好一個介面
https://ithelp.ithome.com.tw/upload/images/20241208/20170494XjTGnm44OP.png

接著把數據接出來 並儲存起來

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox cb_marry;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        findViewById(R.id.btn_save).setOnClickListener(this);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        cb_marry = findViewById(R.id.cb_marry);

        preferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        reload();
    }

    private void reload() {
        String name = preferences.getString("name", null);
        if(name!=null){
            et_name.setText(name);
        }
        Integer age = preferences.getInt("age", 0);
        if(age!=0){
            et_age.setText(String.valueOf(age));
        }
        Float weight = preferences.getFloat("weight", 0f);
        if(weight!=0){
            et_weight.setText(String.valueOf(weight));
        }
        Float height = preferences.getFloat("height", 0f);
        if(height!=0){
            et_height.setText(String.valueOf(height));
        }
        boolean marry = preferences.getBoolean("marry", false);
        if(marry){
            cb_marry.setChecked(true);
        }
    }

    @Override
    public void onClick(View view) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String weight = et_weight.getText().toString();
        String height = et_height.getText().toString();

        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("name", name);
        editor.putInt("age",Integer.parseInt(age));
        editor.putFloat("weight", Float.parseFloat(weight));
        editor.putFloat("height", Float.parseFloat(height));
        editor.putBoolean("marry", cb_marry.isChecked());
        editor.commit();
        String desc = String.format("%s您好,您今年%s歲,身高%s公分,體重%s公斤,已經幫你紀錄好你的數據下次開啟時會自動幫您填好",name,age,height,weight);
        Toast.makeText(this,desc, Toast.LENGTH_SHORT).show();
    }
}

這樣上次填寫的下次就會顯示出來了


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言