输入对话框在android中没有返回值

时间:2014-06-17 13:34:36

标签: android android-dialog

你好我想从android警告对话框返回一些东西,这里是代码

AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("User's Input");
    builder.setMessage(msg);
    final EditText input = new EditText(c);
    input.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
    builder.setView(input);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            text=input.getText().toString();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //dialog.cancel();
            c.finish();
        }
    });

    builder.setCancelable(false);
    builder.show();
    return text;

但是当我这样称呼这个方法时..

String email=MyMethods.showTextDialog(this,"Enter Recovery Email:");
    if(email=="")
        this.finish();

在我按下对话框中的任何按钮之前执行if()语句...我想在对话框按钮点击后执行代码..请帮帮我

4 个答案:

答案 0 :(得分:0)

Create a method like this :

public Dialog showDialog(String title, String msg, final Activity activity) {

        final AlertDialog alertDialog = new AlertDialog.Builder(activity)
                .create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(msg);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();

                    activity.finish();

            }
        });
        alertDialog.show();

        return alertDialog;

    }

答案 1 :(得分:0)

如果要在对话框按钮后执行代码,请单击...然后尝试如下...

将您的代码放在对话框的onClick事件中......

    AlertDialog.Builder builder = new AlertDialog.Builder(c);
    builder.setTitle("User's Input");
    builder.setMessage(msg);
    final EditText input = new EditText(c);
    input.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
    builder.setView(input);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            text=input.getText().toString();

            // put your code here                   
            if(text.equals(""))
                  this.finish();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            //dialog.cancel();
            c.finish();
        }
    });

    builder.setCancelable(false);
    builder.show();

答案 2 :(得分:0)

您可以点击按钮点击对话框或单击编辑文本,然后显示如下对话框

button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


                LayoutInflater li = LayoutInflater.from(context);
                View dialog= li.inflate(R.layout.dialog_view, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);


                alertDialogBuilder.setView(dialog);

                final EditText value= (EditText) dialog
                        .findViewById(R.id.value);


                alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {


                        edittext.setText(value.getText());
                        }
                      })
                    .setNegativeButton("Cancel",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                        }
                      });


                AlertDialog alertDialog = alertDialogBuilder.create();


                alertDialog.show();

            }
        });

答案 3 :(得分:0)

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) {
  String value = input.getText();
  // Do something with value!
  }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    // Canceled.
  }
});

alert.show();

来源:http://www.androidsnippets.com/prompt-user-input-with-an-alertdialog