簡體   English   中英

如何在Android應用程序的警報對話框中為用戶輸入編碼?

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

我有一個帶有兩個按鈕的android應用。 因此,基本上我想做的是,當用戶單擊頁面上的“共享按鈕”時,將出現一個警告對話框,提示用戶輸入其電子郵件地址。 這是我到目前為止所擁有的。 我已經嘗試過EditView,但是對我來說效果不好。

提前致謝!

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

}

}

這可能對您有幫助。

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

                        });

此代碼使用Edittext值創建對話框。

你可以做這樣的事情。

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