How can I display values from EditText user input?

◇◆丶佛笑我妖孽 提交于 2019-12-25 03:38:31

问题


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

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

}

Thank you for your ample time.


回答1:


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

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

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.




回答2:


I'm not sure I really understand your question but are you trying to update the TextView of a different Activity from the one you are in? In which case I don't think this can be done and you should use Intents to pass the data to the activity



来源:https://stackoverflow.com/questions/10233751/how-can-i-display-values-from-edittext-user-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!