how to use alertdialog

戏子无情 提交于 2020-01-16 16:27:40

问题


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..


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/14910602/how-to-use-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!