【问题标题】:How to save user input in dialogue box?如何在对话框中保存用户输入?
【发布时间】:2014-07-22 03:04:25
【问题描述】:

我基本上是在尝试创建一个对话框,将用户输入文本作为“注释”并保存注释并在下次打开对话框时显示注释,然后用户可以添加到注释或从笔记中取出并再次保存。非常感谢您提前。

这是我目前所拥有的:

AlertDialog.Builder alert = new AlertDialog.Builder(ViewTask.this);
alert.setTitle("Task Note");
alert.setMessage("Please enter your note");

// Set an EditText view to get user input 
final EditText input = new EditText(ViewTask.this);
alert.setView(input);
alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText().toString();
        //String usernote = value;
    }
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    }
});
alert.setCancelable(false);
alert.show();
break;

【问题讨论】:

  • 你能描述什么不起作用吗?
  • 嗨@Takendarkk,一切正常,但我不知道如何保存用户输入并在重新打开对话框时调用它。我希望可能是一个参考或方法名称,我可以查看一些见解。非常感谢您的回复。
  • 需要长期保存吗?就像用户可以退出您的应用,然后在 2 天后返回并仍然看到它?
  • 嘿@Takendarkk,是的,我希望能长期保存它。我想我需要一个我相信的数据库?我不知道。感谢您的回复。
  • 查看此链接 --> Android Storage Options

标签: java android eclipse


【解决方案1】:

您正在创建一个新字符串并为其分配值。用户单击“确定”后,字符串将从内存中消失。而是在你的类中创建一个局部变量,并在 clicklistener 中设置它

...
private String value;

...

alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        value = input.getText().toString();
    }
});

如果你想保存它,你可以像这样将局部变量设为静态。

public static value;

然后,您可以通过调用 YourActivity.value 来访问所有类的值。但要注意,如果你使用静态方法,字符串会一直占用内存中的空间,直到你设置它value = null

【讨论】:

  • OP 声明“我希望长期存储它” 如用户可以关闭应用程序,等待几天,打开应用程序,然后仍然可以看到那个笔记。您的解决方案不这样做。
  • Opps,有点快,没看到。他可以通过将值保存在 SharedPreferences 中来解决这个问题。我看到您发布的链接已经引用了它。希望他设法解决它:)
【解决方案2】:

就像 Ibrahim 所说,一旦您的对话框关闭,您将丢失字符串。 为了使用它执行更多操作,例如将其保存到数据库或保存到 sharedPreferences,您应该将它传递回创建此对话框的 Activity,以便它可以处理。

来自Passing events Back To The Dialog's Host

在您的 Dialog 或 DialogFragment 类中,添加一个公共接口以及您可以从 Dialog 调用的方法。

public class MyDialogFragment extends DialogFragment{

    private DialogListener myListener;
    private String text

    public interface DialogListener {
        public void onDialogAction(String text);

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify the host activity implements the callback interface
        try{
            myListener = (DialogListener) activity;
        } catch(ClassCastException e) {
            throw new ClassCastException(activity.toString()
                + " must implement DialogListener");

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setTitle("Task Note");
        alert.setMessage("Please enter your note");

        // Set an EditText view to get user input 
        final EditText input = new EditText(ViewTask.this);
        alert.setView(input);
        alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String value = input.getText().toString();
                myListener.onDialogAction(value);
            }
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });
        alert.setCancelable(false);
        return alert.create()
}

现在你必须改变你的调用活动来实现我们刚刚在 MyDialogFragment 中创建的回调监听器

public class MainActivity extends Activity
                      implements NoticeDialogFragment.NoticeDialogListener{

    private String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        SharedPreferences prefs = thist.sharedPreferences("com.example.app",
            Context.MODE_PRIVATE);
        text = prefs.getString("text_to_save", "");
    }        

    ...
    public void showDialog(){
        DialogFragment dialog = new DialogFragment();
        dialog.show(getFragmentManager(), "MyDialog");
    }

    @Override
    public void onDialogAction(String text){
        // Save the String for next time
        SharedPreferences prefs = this.getSharedPreferences("com.example.app",
            Context.MODE_PRIVATE);
        prefs.edit().putString("text_to_save", text).apply();
    }

如果你想向你的对话框传递一些数据,比如你之前从中得到的文本,你可以实现这个answer

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 1970-01-01
    • 2018-10-30
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多