MessageBox.Show()在android中

时间:2015-06-05 16:45:52

标签: android message alertdialog

我试图在c#中创建类似MessageBox的类。我准备这堂课:

public class MessageBox
{
    public enum MessageBoxResult
    {
        Positive, Negative, Ignore, Cancel, Closed
    };

    private static MessageBoxResult result;
    private static String stringResult;

    public static MessageBoxResult Show(Context context, String title, String message, String positiveMessage, String negativeMessage)
    {
        result = MessageBoxResult.Closed;

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

        builder.setTitle(title);
        builder.setMessage(message);

        builder.setPositiveButton(positiveMessage, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // Do nothing but close the dialog
                result = MessageBoxResult.Positive;
                //dialog.dismiss();

            }

        });

        builder.setNegativeButton(negativeMessage, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                result = MessageBoxResult.Negative;

                //dialog.dismiss();
            }
        });

        AlertDialog alert = builder.create();
        alert.show();

        return result;
    }


    // return password
    public static String ShowPasswordBox(
            Context context, String title, String message, String positiveMessage, String negativeMessage)
    {
        stringResult = null;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);

        // Set up the input
        final EditText input = new EditText(context);

        // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
        input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        builder.setView(input);

        // Set up the buttons
        builder.setPositiveButton(positiveMessage, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                stringResult = input.getText().toString();

                //dialog.dismiss();
            }
        });
        builder.setNegativeButton(negativeMessage, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //dialog.cancel();
                //dialog.dismiss();
            }
        });


        builder.create().show();

        return stringResult;
    }
}

在我的代码中,我首先调用showPasswordBox,然后调用show,但没有任何提示输入密码的提示,我看到show方法,然后看到输入密码的提示。这是我在onClick事件按钮中的代码:

    String s = MessageBox.ShowPasswordBox(this, "password", "enter pass", "ok", "cancel");

    MessageBox.show(this, "show", s,"ok" , "cancel");

我问了这个问题。没有人回答,所以现在我需要一个类,如{#1}} c#。

1 个答案:

答案 0 :(得分:0)

这对我有用,如果您对此答案有疑问,请告诉我。

描述:这个名为MessageBox的类与c#中的MessageBox类相似。它的方法会强制用户输入/选择。

public class MessageBox
{
    public enum MessageBoxResult
    {
        Positive, Negative, Ignore, Cancel, Closed
    };

    private static MessageBoxResult yesNoDialogResult;
    public static MessageBoxResult showYesNo(
            Context context, String title, String message, String positiveMessage, String negativeMessage )
    {
        yesNoDialogResult = MessageBoxResult.Closed;

        // make a handler that throws a runtime exception when a message is received
        final Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message mesg)
            {
                throw new RuntimeException();
            }
        };

        // make a text input dialog and show it
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title);
        alert.setMessage(message);

        alert.setPositiveButton(positiveMessage, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                yesNoDialogResult = MessageBoxResult.Positive;
                handler.sendMessage(handler.obtainMessage());
            }
        });
        alert.setNegativeButton(negativeMessage, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                yesNoDialogResult = MessageBoxResult.Negative;
                handler.sendMessage(handler.obtainMessage());
            }
        });

        alert.show();

        // loop till a runtime exception is triggered.
        try { Looper.loop(); }
        catch(RuntimeException e2) {}

        return yesNoDialogResult;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    private static MessageBoxResult okDialogResult;
    public static MessageBoxResult showOk(
            Context context, String title, String message, String okMessage)
    {
        okDialogResult = MessageBoxResult.Closed;

        // make a handler that throws a runtime exception when a message is received
        final Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message mesg)
            {
                throw new RuntimeException();
            }
        };

        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title);
        alert.setMessage(message);

        alert.setPositiveButton(okMessage, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {
                okDialogResult = MessageBoxResult.Positive;
                handler.sendMessage(handler.obtainMessage());
            }
        });

        AlertDialog dialog = alert.show();


        // align button to center
        Button b = (Button) dialog.findViewById(android.R.id.button1);
        b.setGravity(Gravity.CENTER_HORIZONTAL);

        // loop till a runtime exception is triggered.
        try { Looper.loop(); }
        catch(RuntimeException e2) {}

        return okDialogResult;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////

    private static String inputBoxDialogResult;
    public static String showInput(
            Context context, String title, String message, String okMessage, boolean isPassword)
    {
        inputBoxDialogResult = null;

        // make a handler that throws a runtime exception when a message is received
        final Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message mesg)
            {
                throw new RuntimeException();
            }
        };

        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle(title);
        alert.setMessage(message);

        final EditText input = new EditText(context);
        input.setFocusable(true);

        // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
        if(isPassword)
        {
            input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
        else
        {
            input.setInputType(InputType.TYPE_CLASS_TEXT);
        }

        alert.setView(input);

        alert.setPositiveButton(okMessage, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) {

                inputBoxDialogResult = input.getText().toString();
                handler.sendMessage(handler.obtainMessage());
            }
        });

        AlertDialog dialog = alert.show();


        // align button to center
        Button b = (Button) dialog.findViewById(android.R.id.button1);
        b.setGravity(Gravity.CENTER_HORIZONTAL);

        // loop till a runtime exception is triggered.
        try { Looper.loop(); }
        catch(RuntimeException e2) {}

        return inputBoxDialogResult;
    }
}