【问题标题】:How can I display values from EditText user input?如何显示来自 EditText 用户输入的值?
【发布时间】:2012-04-19 17:44:30
【问题描述】:

我有一个 SharedPreferences,它保存来自一个活动的 EditText 输入并在另一个活动中显示字符串值。

当我在 EditText 字段中输入输入并按下(我创建的按钮)“保存”(将 EditText 提交到编辑器)时,文件已成功存储。但是,显示活动(显示 SharedPreferences 中存储的字符串值)不显示这些值。我猜是因为它是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);

}

感谢您的充裕时间。

【问题讨论】:

    标签: android sharedpreferences


    【解决方案1】:

    我认为您正在寻找的部分内容是将您的 CustomStoreActivity 代码放入 onResume 而不是 onCreate。无论何时将CustomStoreActivity 置于最前面(包括首次创建时),您移入onResume 的任何代码都会出现。

    另一种选择,假设CustomStoreActivity 用于启动包含EditTexts 的Activity,是使用startActivityForResult 并将数据传回结果Intent,而不是使用@987654333 @。 Here 是一个简单的示例(但请注意,此示例中的 setResult 调用是通过 null 传递的,您希望在其中传递 Intent,如 Android 文档中的 here 所述)。

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

    【讨论】:

    • 成功了,非常感谢乔恩。我不知道我可以从另一个 MapActivity 重新调用 OnResume()。谢谢。
    • 不客气;不过,您永远不必手动调用onResume。我假设您正在查看 CustomStoreActivity 以启动,然后启动编辑器 Activity,在这种情况下,当您在前台关闭 Activity(您的重新用于更新文本字段)。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      相关资源
      最近更新 更多