亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

每次單擊在Android Studio中來回切換活動時如何將值增加一

每次單擊在Android Studio中來回切換活動時如何將值增加一

臨摹微笑 2023-04-19 15:57:53
每次單擊按鈕來回切換活動時,我都試圖將變量計數的值增加一。但是,每次我單擊時,計數值仍然相同。有人可以幫忙嗎?我正在嘗試放置一些生命周期 android 代碼,但它仍然沒有任何區別。package com.darayuth.learning;import android.content.Context;import android.content.Intent;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.squareup.picasso.Picasso;public class MainActivity extends AppCompatActivity {    public static final String EXTRA_MESSAGE = "com.darayuth.learning";    public static final String TAG = "MainActivity";    public static final String value = "value";    private int count = 0;    private TextView textView;    @Override    protected void onSaveInstanceState(Bundle outState) {        outState.putInt(value, count);        super.onSaveInstanceState(outState);    }    @Override    protected void onRestoreInstanceState(Bundle savedInstanceState) {        super.onRestoreInstanceState(savedInstanceState);        count = savedInstanceState.getInt(value);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = findViewById(R.id.textView2);        Button btn = findViewById(R.id.button);        btn.setOnClickListener((View v)->{            sendMessage();        });    }    public void sendMessage(){        count = count + 1;        Log.i(TAG, "value: " + count);        Intent intent = new Intent(this, Main2Activity.class);        EditText editText = findViewById(R.id.editText);        String message = editText.getText().toString();        intent.putExtra(EXTRA_MESSAGE, message );        startActivity(intent);    }}</LinearLayout>
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

我建議使用 SharedPreferences 來存儲和檢索活動之間的值。


private void storeValue(){

? ? SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);


? ? // Store an integer using the Shared Preferences editor

? ? SharedPreferences.Editor editor = prefs.edit();

? ? editor.putInt("myPreferenceValueKey", value); // Store the value with a key to identify it

? ? editor.apply();

}


private void retrieveValue(){

? ? SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

? ? int defaultValue = 0; // This is the value that's returned if the preference doesn't exist


? ? // Retrieve the stored value using the associated key

? ? value = prefs.getInt("myPreferenceValueKey", defaultValue);

}

在您的第一個活動中,只需在增加值之后但在開始下一個活動之前調用retrieveValue()您的onCreate()then 調用。storeValue()


您可以在第二個 Activity 中創建相同的函數來根據需要存儲和檢索值。

查看完整回答
反對 回復 2023-04-19
  • 1 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號