如何在 Android 中持久更改 TextView 文本?

How to change TextView text persistently in Android?

我试过使用 Thread,但没有成功,在我使用 EditText 作为输入更改 TextView 文本后,textView 没有改变。请帮助我!

        Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            //shared is SharedPreferences object that I define as an instance variable
            String inp = shared.getString("input", def);

            textView.setText(inp);
            Log.d("input",inp);
    });

    thread.start();

为什么要在单独的线程中进行。即使您愿意,也无法在非 UI 线程中更新任何 UI 组件,例如 textView。

试试这个:

    Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        //shared is SharedPreferences object that I define as an instance variable
        String inp = shared.getString("input", def);
         runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        textView.setText(inp);
                        Log.d("input",inp);
                    }
                });
});

thread.start();

要更新 UI,您应该使用主 ui 线程。看看 :

Android runOnUiThread explanation