Reputation: 5904
I'm using TextToSpeech
for the first time and i have a problem.. I created a dialog with a editText where i can write something:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
I need save in preferences that value.
I created this one:
private void prefvalue(String tag) {
String name = savename.getString(Tag, null);
SharedPreferences.Editor preferencesEditor = savename.edit();
preferencesEditor.putString("tag", tag);
preferencesEditor.commit();
}
well.. now in onCreate what have i to do? Because i tried do something like
input.setText(savename.getString("tag", Default Value"));
where input
is the editText inside the AlertDialog. But of course doesn't work because it doesn't find the input
variable.
Upvotes: 0
Views: 1932
Reputation: 18923
you can save that value on Click of positive button.
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
SharedPreferences sp = getSharedPreference("MyKey",0);
SharedPreferences.Editor preferencesEditor = sp.edit();
preferencesEditor.putString("tag", value);
preferencesEditor.commit();
}
});
Now retrieve anywhere where you want.
SharedPreferences sp = getSharedPreference("MyKey",0);
String data = sp.getString("tag","");
Now set this data string to your EditText.
Upvotes: 1