尝试使用Java Android Studio永久固定

发布于 2025-01-23 20:45:14 字数 389 浏览 3 评论 0原文

我正在尝试使用以下代码进行setText,该代码正常工作,但是当我更改活动然后恢复到活动时,SetText似乎重置。还有另一种解决这个问题的方法吗?

       public void onClick(View v) {
            String Note = editTextNote.getText().toString();
            String Email = mAuth.getCurrentUser().getEmail();
            String Status = ("IN");
            
            
            tvClockingStatus.setText("You are clocked: IN");

I am trying to setText using the following code, the code works fine however when I change activity and then revert back to the activity, the setText seems to reset. Is there another method to resolve this?

       public void onClick(View v) {
            String Note = editTextNote.getText().toString();
            String Email = mAuth.getCurrentUser().getEmail();
            String Status = ("IN");
            
            
            tvClockingStatus.setText("You are clocked: IN");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2025-01-30 20:45:14

在普通应用程序生命周期中的不同时间创建和破坏了Android中的活动(例如,旋转屏幕或导航时)。如果您希望数据持续时间比活动的生命周期更长,则需要将其存储在更永久的地方。

存储少量数据的一个常见地点是 sharedpreferences 将键值对写入磁盘上,以便将数据保存,即使应用程序被停止并重新启动,或者活动被破坏并重新创建。

这是一个简单的示例,说明如何使用sharedPreferences保存“时钟”状态字符串。在on Resume调用数据中,数据是从保存的首选项中加载的,并应用于文本视图。

public class MainActivity extends AppCompatActivity {

    private SharedPreferences prefs;
    private TextView status;
    private static final String statusKey = "CLOCKED_STATUS";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        Button clockIn = findViewById(R.id.clock_in);
        Button clockOut = findViewById(R.id.clock_out);
        status = findViewById(R.id.clocking_status);

        clockIn.setOnClickListener(v -> {
            updateStatus("Clocked In");
        });

        clockOut.setOnClickListener(v -> {
            updateStatus("Clocked Out");
        });
    }

    private void updateStatus(String newStatus) {
        // save the new state in the preferences - can use 
        // putInt, putBoolean, or several other options to 
        // save different data types
        SharedPreferences.Editor ed = prefs.edit();
        ed.putString(statusKey, newStatus);
        ed.apply();

        // Update the data shown in the TextView
        status.setText(newStatus);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Load the saved status and show it in the TextView
        String defaultStatus = "Clocked Out";
        String savedStatus = prefs.getString(statusKey, defaultStatus);
        status.setText(savedStatus);
    }
}

Activities in Android are created and destroyed at various times in the normal application lifecycle (e.g. when you rotate the screen, or navigate away). If you want data to persist longer than the lifecycle of an Activity you need to store it somewhere more permanent.

One common place to store small amounts of data is SharedPreferences, which writes key-value pairs to disk so the data is saved even if the app is stopped and restarted or if the activity is destroyed and recreated.

Here is a simple example of how you could save a "clocked in" status string using SharedPreferences. In the onResume call the data is loaded from the saved preferences and applied to the TextView.

public class MainActivity extends AppCompatActivity {

    private SharedPreferences prefs;
    private TextView status;
    private static final String statusKey = "CLOCKED_STATUS";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        Button clockIn = findViewById(R.id.clock_in);
        Button clockOut = findViewById(R.id.clock_out);
        status = findViewById(R.id.clocking_status);

        clockIn.setOnClickListener(v -> {
            updateStatus("Clocked In");
        });

        clockOut.setOnClickListener(v -> {
            updateStatus("Clocked Out");
        });
    }

    private void updateStatus(String newStatus) {
        // save the new state in the preferences - can use 
        // putInt, putBoolean, or several other options to 
        // save different data types
        SharedPreferences.Editor ed = prefs.edit();
        ed.putString(statusKey, newStatus);
        ed.apply();

        // Update the data shown in the TextView
        status.setText(newStatus);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Load the saved status and show it in the TextView
        String defaultStatus = "Clocked Out";
        String savedStatus = prefs.getString(statusKey, defaultStatus);
        status.setText(savedStatus);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文