我怎么可以显示的EditText用户输入值? [英] How can I display values from EditText user input?

查看:241
本文介绍了我怎么可以显示的EditText用户输入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共享preferences从一个活动节省的EditText输入并显示在另一活动中的字符串值。

I have a SharedPreferences that saves EditText input from one activity and displays the String value in another activity.

当我输入一个投入的EditText字段和preSS(一个按钮,我创建)保存(这将提交到的EditText编辑器),该文件存储成功。然而,显示的活性(它显示在共享preferences所存储的字符串值)不显示这些值。我猜测这是因为它是的onCreate,所以屏幕被创造在运行时。我怎样才能得到它,当用户提交(presses保存)更新的EditText值立刻?

When I enter an input into the EditText fields and press (a button I created) "Save" (which commits the EditText to editor), the file is stored successfully. However, the display activity (which displays the stored String values in SharedPreferences) doesn't display the values. I am guessing it is because it is the onCreate, so the screen is "created" when it runs. How can I get it to update the EditText values when the user commits (presses save) immediately?

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

CustomStoreEditActivity - Just storing the user inputs (EditText entries):

    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 - 在那里,我相信问题所在:

CustomStoreActivity - where I believe the problem lies:

    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);

}

感谢您有充足的时间。

推荐答案

我想你要找的内容是把你的CustomStoreActivity code在 onResume 而不是的onCreate 。无论code你进入 onResume 将发生每当 CustomStoreActivity 被带到前面(包括当它是首先创建)。

I think part of what you're looking for is to put your CustomStoreActivity code in onResume instead of onCreate. Whatever code you move into onResume will occur whenever the CustomStoreActivity is brought to the front (including when it is first created).

另一种方法,假设 CustomStoreActivity 用于启动活动包含的EditText S,是使用 startActivityForResult 和数据传回的结果意图代替使用的共享preferences 这里是一个简单的例子(但要注意的的setResult 呼叫在此示例传递在这里,你会想在意图,如记录这里在Android文档)。

Another alternative, assuming that CustomStoreActivity is used to launch the Activity that contains the EditTexts, is to use startActivityForResult and pass the data back in the result Intent instead of using the SharedPreferences. Here is a simple example (though note that the setResult call in this example is passed null where you would want to pass in an Intent, as documented here in the Android docs).

它也好像你正在尝试使用你的第二个活动像可编辑字段的对话框。如果是这样的情况下,另一种选择是实际使用对话框类的一个变种你的 CustomStoreActivity 类中,而不是创建另一个活动来像之一。看到Android的文档的对话框

It also seems like you're trying to use your second Activity like a dialog box with editable fields. If that's the case, yet another alternative is to actually use a variant of the Dialog class within your CustomStoreActivity class instead of creating another Activity to act like one. See the Android doc for dialogs.

这篇关于我怎么可以显示的EditText用户输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆