如何显示EditText用户输入的值?

时间:2012-04-19 17:44:31

标签: android sharedpreferences

我有一个SharedPreferences,它从一个活动中保存EditText输入,并在另一个活动中显示String值。

当我在EditText字段中输入一个输入并按下(我创建的一个按钮)“Save”(将EditText提交给编辑器)时,文件存储成功。但是,显示活动(在SharedPreferences中显示存储的String值)不显示值。我猜它是因为它是onCreate,所以屏幕在运行时“创建”。如何在用户提交(按下保存)时立即更新EditText值?

CustomStoreEditActivity - 只存储用户输入(EditText条目):

    final Button saveButton = (Button) findViewById(R.id.saveButton);
    saveButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            if (saveButton.isClickable()) {
                SharedPreferences prefs = getSharedPreferences(
                        "customtime", 0);
                // prefs.registerOnSharedPreferenceChangeListener(this);
                final SharedPreferences.Editor edit = prefs.edit();
                EditText shopName = (EditText) findViewById(R.id.shopName);
                EditText open1 = (EditText) findViewById(R.id.open1);
                EditText close1 = (EditText) findViewById(R.id.close1);
                EditText open2 = (EditText) findViewById(R.id.open2);
                EditText close2 = (EditText) findViewById(R.id.close2);
                EditText open3 = (EditText) findViewById(R.id.open3);
                EditText close3 = (EditText) findViewById(R.id.close3);
                EditText open4 = (EditText) findViewById(R.id.open4);
                EditText close4 = (EditText) findViewById(R.id.close4);
                EditText open5 = (EditText) findViewById(R.id.open5);
                EditText close5 = (EditText) findViewById(R.id.close5);
                EditText open6 = (EditText) findViewById(R.id.open6);
                EditText close6 = (EditText) findViewById(R.id.close6);
                EditText open7 = (EditText) findViewById(R.id.open7);
                EditText close7 = (EditText) findViewById(R.id.close7);
                EditText comments = (EditText) findViewById(R.id.comments);
                edit.putString("shopName", shopName.getText().toString());
                edit.putString("Monday1", open1.getText().toString());
                edit.putString("Monday2", close1.getText().toString());
                edit.putString("Tuesday1", open2.getText().toString());
                edit.putString("Tuesday2", close2.getText().toString());
                edit.putString("Wednesday1", open3.getText().toString());
                edit.putString("Wednesday2", close3.getText().toString());
                edit.putString("Thursday1", open4.getText().toString());
                edit.putString("Thursday2", close4.getText().toString());
                edit.putString("Friday1", open5.getText().toString());
                edit.putString("Friday2", close5.getText().toString());
                edit.putString("Saturday1", open6.getText().toString());
                edit.putString("Saturday2", close6.getText().toString());
                edit.putString("Sunday1", open7.getText().toString());
                edit.putString("Sunday2", close7.getText().toString());
                edit.putString("comments", comments.getText().toString());
                edit.commit();
                Intent myIntent = new Intent(getBaseContext(),
                        CustomStoreActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                startActivity(myIntent);

                Toast.makeText(getBaseContext(), "Opening Hours Saved!",
                        Toast.LENGTH_SHORT).show();

            }

        }
    });

}

CustomStoreActivity - 我认为问题所在:

    private void displayPreferences(){

    SharedPreferences prefs = getSharedPreferences("customtime", 0);
    String shopName = prefs.getString("shopName", "Empty");
    String shopTime1 = prefs.getString("Monday1", " ");
    String shopTime2 = prefs.getString("Monday2",  " ");
    String shopComments = prefs.getString("comments", "");

    TextView displayPrefs = (TextView) findViewById(R.id.displayPrefs);

    displayPrefs.setText(shopName + shopTime1 + shopTime2 + shopComments);

}

感谢您充裕的时间。

2 个答案:

答案 0 :(得分:1)

我认为您要查找的部分内容是将CustomStoreActivity代码放在onResume而不是onCreate中。无论何时onResume被带到前面(包括首次创建时),您都会进入CustomStoreActivity移动的代码。

假设CustomStoreActivity用于启动包含Activity的{​​{1}},另一种方法是使用EditText并在结果中传回数据{ {1}}而不是使用startActivityForResultHere是一个简单的示例(但请注意,此示例中的Intent调用已传递SharedPreferences,您希望传入setResult,如文档{{3}在Android文档中。)

您似乎也尝试使用第二个null,就像一个带有可编辑字段的对话框。如果是这种情况,另一种方法是在Intent类中实际使用Activity类的变体,而不是创建另一个Dialog来表现。请参阅Android文档here

答案 1 :(得分:0)

我不确定我是否真的理解您的问题但是您是否尝试更新与您所在的Activity不同的TextView?在这种情况下,我不认为这可以做到,你应该使用Intents将数据传递给活动