android - TextView 未在 Android 的 onResume 中更新

标签 android android-layout textview android-linearlayout onresume

我的代码中的 textview 有问题,当主要 Activity 恢复时它没有更新。基本上它有一个 TextView 和一个按钮。当程序加载时,它从 sharedpreferences 文件中读取日期并设置 textview 文本。当您单击该按钮时,它会启动一个新 Activity ,该 Activity 具有一个日期选择器,允许您选择要保存在 sharedpreferences 文件中的日期。当您单击确定或按更新 Activity 的后退按钮时,我试图让 textview 更新新日期(如果它已更改)但每次运行 onresume 时它都不会更新 textview 文本。我曾尝试使主要线性布局无效以重绘它,但这不起作用。我已尝试使用 startactivityforresult 启动 Activity ,即使在处理 onactivityresult 中的 textview 更新时也不起作用。

这是主类的代码:

public class ASMain extends Activity implements OnClickListener {

    private LinearLayout llMain;
    private TextView tvDate;
    private Button btnDate;
    private SharedPreferences spSettings;
    private static final String SETTINGS_FILE = "MySettings";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        llMain = (LinearLayout)findViewById(R.id.llMain);
        tvDate = (TextView)findViewById(R.id.tvDate);
        btnDate = (Button)findViewById(R.id.btnDate);
        spSettings = getSharedPreferences(SETTINGS_FILE,0);

        tvDate.setText(spSettings.getString("Date", "ERROR"));
        btnDate.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        spSettings = getSharedPreferences(SETTINGS_FILE, 0);
        tvDate.setText(spSettings.getString("Date", "ERROR"));
        super.onResume();
    }

    @Override
    public void onClick(View v) {
      if (v.Id == R.id.btnDate)
      {
        Intent intent = new Intent(this, update.class);
        startActivity(intent);
    }
  }
}

这是 update.class 代码:

public class update extends Activity implements OnClickListener {

    private LinearLayout llUpdate;
    private TextView tvLabel;
    private DatePicker dpDate;
    private Button btnOK;
    private SharedPreferences spSettings;
    private static final String SETTINGS_FILE = "MySettings";

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        llUpdate = (LinearLayout) findViewById(R.id.llUpdate);
        tvLabel = (TextView) findViewById(R.id.tvLabel);
        dpDate = (DatePicker) findViewById(R.id.dpDate);
        btnOK = (Button) findViewById(R.id.btnOK);
        spSettings = getSharedPreferences(SETTINGS_FILE, 0);
        String sTemp = spSettings.getString("Date", "01/01/2013");
        dpDate.updateDate(Integer.parseInt(sTemp.substring(6, 9)), Integer.parseInt(sTemp.substring(0, 1)), Integer.parseInt(sTemp.substring(3, 4)));
        btnOK.setOnClickListener(this);
    }

    @Override
    public void onStop() {
        SharedPreferences.Editor speSettings = spSettings.edit();
        speSettings.putString("Date", String.valueOf(dpDate.getMonth()) + "/" + String.valueOf(dpDate.getDayOfMonth()) + "/" + String.valueOf(dpDate.getYear()));
        speSettings.commit();

        super.onStop();
    }

    @Override
    public void onClick(View v) {
        if (v.Id = R.id.btnOK)
        {
            this.finish();
        }
    }
}

如果我再次单击该按钮,textview 将更新,但是一旦我返回主 Activity ,我在开始的 Intent 中所做的任何更改都不会显示。

最佳答案

像下面这样改变你的onResume()..

@Override
protected void onResume() {
    spSettings = getSharedPreferences(SETTINGS_FILE, 0);
    tvDate.setText(spSettings.getString("Date", "ERROR"));
    super.onResume();
}

关于android - TextView 未在 Android 的 onResume 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18648042/

相关文章:

android - 通过 SmsManager 发送的 SMS 不会自动保存在 SENT 文件夹中?

java - 在 java 中使用多个 WHERE 子句更新 sql 数据库

android - 在使用 Button 调试 NullPointerException 时需要帮助

android - Gif 在自定义 ListView 中没有动画

android - 将整个 Textview 旋转 -90 度

android - 单元测试使用 ViewModelScope.launch 调用具有延迟的挂起函数的 ViewModel 方法

android - 在 Android 中使用预览绘制圆形 GeoFence

android - 如何在 Android 布局中将 TEXTVIEW 中心放在 IMAGEVIEW 上

android - drawableEnd textview 属性在移动 android 中不起作用

android - 多个 TextViews 更新很慢