按下按钮创建简单的确认对话框 - Android

时间:2014-03-15 12:54:37

标签: android

我正在创建一个简单的秒表应用程序,并且有一个按钮,可以在按下按钮时停止计时器。很简单,见下文。

我想创建一个弹出的确认对话框,询问用户是否确定要停止计时器。我怎么能这样做?

  

public void stopClick(查看视图){

  chronometer.stop();

}

更新:

谢谢。我现在有以下代码但收到错误。同时发布以下日志。

  

public void stopClick(查看视图){

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(R.string.app_name);
    builder.setMessage("Do you want to stop ?");
    builder.setIcon(R.drawable.ic_launcher);
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
            chronometer.stop();    // stop chronometer here

        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

日志:

03-15 09:21:49.130: I/Process(27748): Sending signal. PID: 27748 SIG: 9
03-15 09:21:53.550: D/AndroidRuntime(28182): Shutting down VM
03-15 09:21:53.550: W/dalvikvm(28182): threadid=1: thread exiting with uncaught exception (group=0x42055898)

03-15 09:21:53.560: E/AndroidRuntime(28182): FATAL EXCEPTION: main
03-15 09:21:53.560: E/AndroidRuntime(28182): java.lang.IllegalStateException: Could not execute method of the activity

03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.view.View$1.onClick(View.java:3838)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.view.View.performClick(View.java:4475)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.view.View$PerformClick.run(View.java:18784)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.os.Handler.handleCallback(Handler.java:730)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.os.Looper.loop(Looper.java:137)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.app.ActivityThread.main(ActivityThread.java:5450)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at java.lang.reflect.Method.invoke(Method.java:525)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at dalvik.system.NativeStart.main(Native Method)
03-15 09:21:53.560: E/AndroidRuntime(28182): Caused by: java.lang.reflect.InvocationTargetException
03-15 09:21:53.560: E/AndroidRuntime(28182):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at java.lang.reflect.Method.invoke(Method.java:525)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.view.View$1.onClick(View.java:3833)
03-15 09:21:53.560: E/AndroidRuntime(28182):    ... 11 more
03-15 09:21:53.560: E/AndroidRuntime(28182): Caused by: java.lang.NullPointerException
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
03-15 09:21:53.560: E/AndroidRuntime(28182):    at steel.timer.MainActivity.stopClick(MainActivity.java:56)
03-15 09:21:53.560: E/AndroidRuntime(28182):    ... 14 more

3 个答案:

答案 0 :(得分:6)

使用AlertDialog.Builder向用户询问确认。

试试这个

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.app_name);
builder.setMessage("Do you want to stop ?");
builder.setIcon(R.drawable.ic_launcher);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.dismiss();
        chronometer.stop();    // stop chronometer here

    }
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.dismiss();
    }
});
AlertDialog alert = builder.create();
alert.show();

答案 1 :(得分:0)

尝试下面的代码来创建对话框

AlertDialog.Builder builder = new AlertDialog.Builder(_context);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
{
    @Override
    public void onClick(DialogInterface dialog, int which) 
    {
        // Stuff to do
    }
});
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() 
    {
        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
               // Stuff to do
        }
    });

    builder.setMessage("Your_MSG");
    builder.setTitle("Warning..");

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

答案 2 :(得分:0)

尝试一下

<强>使用该警报()方法点击按钮

/* this method for stop Alert app  */
private void Alert() {
    android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this);
    String titleText = "App Name";
    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.BLACK);
    SpannableStringBuilder ssBuilder = new SpannableStringBuilder(titleText);
    ssBuilder.setSpan(
            foregroundColorSpan,
            0,
            titleText.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    );
    builder.setCancelable(false);
    builder.setTitle(ssBuilder);
    builder.setMessage("Do you want to stop ?");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            chronometer.stop();    // stop chronometer here
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    // Create the alert dialog
    android.support.v7.app.AlertDialog dialog = builder.create();
    // Finally, display the alert dialog
    dialog.show();
    // Get the alert dialog buttons reference
    Button positiveButton = dialog.getButton(android.support.v7.app.AlertDialog.BUTTON_POSITIVE);
    Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
    // Change the alert dialog buttons text and background color
    positiveButton.setTextColor(Color.parseColor("#00aff1"));
    negativeButton.setTextColor(Color.parseColor("#00aff1"));

}