简体   繁体   中英

Returning A Value From AlertDialog

I want to build a function that creates an AlertDialog and returns the string that the user entered, this the function I have for creating the dialog, how do I return the value?

String m_Text = "";
private String openDialog(String title) {
    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
    builder.setTitle(title);

    final EditText input = new EditText(view.getContext());
    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
    builder.setView(input);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            m_Text = input.getText().toString();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.show();

// return string
} 

The call builder.show() which opens your AlertDialog is not a blocking call, meaning the next instructions will be executed without waiting for the AlertDialog to finish (return). The way you should interact with it is by using some sort of callback . For instance, your OnClickListeners are an implementation of such a pattern.

A simple callback pattern

One clean way to achieve what you want is to create a Functional Interface which is an interface having only one method. You would use it for handling your callbacks.

Example

interface OnOK{
    void onTextEntered(String text);
}

And then you would alter you method to be like:

private void openDialog(String title, final OnOK onOK) {
    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
    builder.setTitle(title);

    final EditText input = new EditText(view.getContext());
    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
    builder.setView(input);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog, int which) {
          //Oi, look at this line!
          onOK.onTextEntered(input.getText().toString());
       }
    });

    builder.show();
} 

You can use it like this:

openDialog("Title", new OnOK() {
   @Override
   onTextEntered(String text) {
      Log.i("LOG", text);
   } 
});

在同一个类中创建另一个接受字符串值的方法,然后从setPositiveButton onclick 事件调用该函数提供input.getText().toString()的值

This looks to me like you have stored the value of the inputted text in the m_Text field. You can either just return that field or have a variable within the function in which you store the value to be returned.

Where you have:

//Return string 

simply replacing with:

return m_Text; 

should do the job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM