how to use alertdialog

2019-08-30 01:24发布

package com.progme.wallkon;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class NextActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.next);

    ImageView im1;
    im1 = (ImageView)findViewById(R.id.a_01_b);
    im1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    ImageView im2;
    im2 = (ImageView)findViewById(R.id.a_02_b);
    im2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    ImageView im3;
    im3 = (ImageView)findViewById(R.id.a_03_b);
    im3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            showDialog(1);
        }
    });
}
            @Override
            protected Dialog onCreateDialog(int id) {

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Gmelon");
                builder.setMessage("setting?");
                builder.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i("MyTag" , "Click YES");
                    }
                });

                builder.setNegativeButton("NO",
                        new android.content.DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i("MyTag", "Click NO");
                    }
                });
                return builder.create();
            }
}

I wrote code in activity.java like this..

I want to use dialog in im1, im2, im3, and each have to get another event. Then, I have to write 3 dialog? and how I can set [//TODO Auto...] here that I use is like.. first dialog for im1, second dialog for im2, third dialog for im3..

Please help..

2条回答
姐就是有狂的资本
2楼-- · 2019-08-30 01:35

You can write a private variable for the alert dialog and reuse it, but not at the same time

private AlertDialog mDialog = new AlertDialog.Builder(this)
            .setTitle("Gmelon")
            .setMessage("setting?")
            .setPositiveButton("YES",
                    new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.i("MyTag" , "Click YES");
                }
            })

            .setNegativeButton("NO",
                    new android.content.DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Log.i("MyTag", "Click NO");
                }
            }).create();

now you can show the dialog where ever you want in your code.

查看更多
男人必须洒脱
3楼-- · 2019-08-30 01:56

It looks like you could just use showDialog(x) to me unless there is more to this question.

查看更多
登录 后发表回答