简体   繁体   English

如何在Android应用程序的警报对话框中为用户输入编码?

[英]How do I code for user input in an alert dialog for an android app?

I have an android app that has two buttons. 我有一个带有两个按钮的android应用。 So basically what I am trying to do is when a user clicks the 'Share Button' on the page, an alert dialog box will appear that will prompt the user to enter their email address. 因此,基本上我想做的是,当用户单击页面上的“共享按钮”时,将出现一个警告对话框,提示用户输入其电子邮件地址。 Here is what I have so far. 这是我到目前为止所拥有的。 I've tried an EditView but it hasn't been working well for me. 我已经尝试过EditView,但是对我来说效果不好。

Thanks in advance! 提前致谢!

package com.colors;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    final Context context = this;
    private Button button;
    private Button shareButton;

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);
    shareButton = (Button) findViewById(R.id.shareButton);

    // add button listener for Welcome Message.
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

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

            // set the title of the Alert Dialog
            alertDialogBuilder.setTitle("Welcome!");

            // set dialog message
            alertDialogBuilder
                    .setMessage("Program Description here...")
                    .setCancelable(false)
                    .setNegativeButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if no is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

            AlertDialog alertDialog = alertDialogBuilder.create();

            alertDialog.show();
        }
    });





    // Button listener for Share Button
            shareButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

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

                    // set the title of the Alert Dialog
                    alertDialogBuilder.setTitle("Share");

                    // set dialog message
                    alertDialogBuilder
                            .setMessage("Would like to a user input here.")
                            .setCancelable(false)
                            .setPositiveButton("Send!",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int id) {
                                            // if no is clicked, just close
                                            // the dialog box and do nothing
                                            dialog.cancel();
                                        }
                                    })
                            .setNegativeButton("Cancel",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int id) {
                                            // if no is clicked, just close
                                            // the dialog box and do nothing
                                            dialog.cancel();
                                        }
                                    });

                    AlertDialog alertDialog = alertDialogBuilder.create();

                    alertDialog.show();
                }
            });

}

} }

This may helpful to you. 这可能对您有帮助。

                        shareButton.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {


                                AlertDialog.Builder al1 = new Builder(
                                        AgriListView.this);
                                al1.setMessage("Share Something");

                                al1.setPositiveButton("Share",
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {

                                                final EditText input = new EditText(
                                                        AgriListView.this);
                                                input.setSingleLine();

                                                AlertDialog.Builder al = new Builder(
                                                        AgriListView.this);
                                                al.setTitle("Enter New Value");
                                                al.setView(input);
                                                al.setCancelable(true);
                                                al.setIcon(R.drawable.bt);
                                                al.setPositiveButton(
                                                        "OK",
                                                        new DialogInterface.OnClickListener() {

                                                            public void onClick(
                                                                    DialogInterface dialog,
                                                                    int which) {

                                                                int len = input
                                                                        .length();

                                                                if (!(len == 0)) {

                                                                    Toast.makeText(
                                                                            AgriListView.this,
                                                                            "Entered text is: "+input.getText()
                                                                                ,
                                                                            Toast.LENGTH_SHORT)
                                                                            .show();

                                                                } else {

                                                                    Toast.makeText(
                                                                            getApplicationContext(),
                                                                            "Enter Value Properly",
                                                                            Toast.LENGTH_LONG)
                                                                            .show();
                                                                }
                                                            }

                                                        });

                                                al.setNegativeButton(
                                                        "Cancel",
                                                        new DialogInterface.OnClickListener() {

                                                            public void onClick(
                                                                    DialogInterface dialog,
                                                                    int which) {
                                                                dialog.cancel();
                                                            }
                                                        });

                                                AlertDialog alert = al.create();
                                                alert.show();
                                            }
                                        });

                                al1.setNegativeButton("Cancel",
                                        new DialogInterface.OnClickListener() {

                                            public void onClick(
                                                    DialogInterface dialog,
                                                    int which) {
                                                dialog.cancel();

                                            }
                                        });

                                AlertDialog alert1 = al1.create();
                                alert1.show();
                            }

                        });

This code create dialog with Edittext value. 此代码使用Edittext值创建对话框。

You could do something like this. 你可以做这样的事情。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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