0
私はアンドロイドを初めて使っていて、100%ではないと確信しています。私の質問の用語は正しいが、ここにある。Androidは別のアクティビティからTextViewを更新します
設定を反映したいTextView
のレイアウトがあります。私が直面している問題は、設定ページ(customsignalsetup)が、TextView
が更新したいページからナビゲートされていることです。設定ページでオプションが変更され、Finish()
を使用して閉じると、TextView
は更新されませんでした。しかし、私がTextView
のページを閉じると、再び開いて更新されました。
ここに私のコードです:
のTextViewアップデートコード(設定ページで変更前)
settings = getSharedPreferences("CustomSignalSettings", 0);
TextView CustLabel = (TextView)findViewById(R.id.cunitslabel);
CustLabel.setText("Custom (" + settings.getString("CustomSignalUnit", "").toString() + ") =");
メニューからプレス「設定」を処理し、設定]ページに
を切り替えるコードpublic boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.custom_signal_setup:
// Open Settings Page
Intent intent = new Intent(PLCActivity.this, customsignalsetup.class);
startActivity(intent);
return true;
case R.id.help:
//get help
return true;
default:
return super.onOptionsItemSelected(item);
}
}
設定ページコードU pdateのTextView
public class customsignalsetup extends AppCompatActivity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customplcsettingspopup);
Button button = (Button) findViewById(R.id.save);
button.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.save:
//THE BELOW LINE IS SUPPOSED TO SET 'CustLabel' TO THE TEXTVIEW TO BE UPDATED
TextView CustLabel = (TextView)findViewById(R.id.cunitslabel);
SharedPreferences settings = getSharedPreferences("CustomSignalSettings", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("CustomSignalUnit",CusUnit.getText().toString());
editor.commit();
// THE BELOW LINE IS SUPPOSED TO UPDATE THE TEXTVIEW ON A DIFFERENT PAGE
CustLabel.setText("Custom (" + settings.getString("CustomSignalUnit", "").toString() + ") =");
finish();
}
}
}
私のコードは、エラーをスローしませんが、TextViewには、単純に更新されません。元のテキストビューを読み込んだアクティビティがまだ終了しておらず、将来のラベルのみが更新されるためですあなたが更新された値を毎回取得するよう
おかげ
ありがとうございます!非常に簡単です! – SilverShotBee