单击按钮后的AlertDialog

时间:2013-10-01 09:22:22

标签: android button android-intent alertdialog

我想在点击按钮(参见下图)后添加 AlertDialog ,该按钮启动活动以选择图片。

我点击按钮选择图片。我选择了图片,我想在选择图片后显示我的 AlertDialog

    final Button myButton = (Button) findViewById(R.id.button);
    myButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MyActivity.this, OpenImageActivity.class);
            startActivity(intent);
        }
    });

如何让我的AlertDialog可见?

感谢。

3 个答案:

答案 0 :(得分:1)

首先,你必须像这样发送意图

final Button myButton = (Button) findViewById(R.id.wall);
myButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intentCROP = new Intent(MyActivity.this, OpenImageListActivity.class);
        //startActivity(intentCROP);
        startActivityForResult(intentCROP, 1);
    }
});

然后点击目标活动上的列表项

    @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Intent intent = new Intent();
    intent.putExtra("code", "response");
    setResult(RESULT_OK, intent);
    finish();

}

现在,回到第一个活动

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {


                            // here you can show your alert dialog


        }
    }

}

答案 1 :(得分:0)

AlertDialog.Builder alert;
alert = new AlertDialog.Builder(this);

&安培;在您的点击方法中执行此操作

alert.setMessage("Title").setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                //do your work      

            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                dialog.cancel();
            }
        });

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

答案 2 :(得分:0)

你可以做这样的事情

mButton1.setOnClickListener(new OnClickListener() {

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

     confirmdialog();
}
 });

现在确认对话方法

protected void confirmdialog() {
    // TODO Auto-generated method stub
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

    // Setting Dialog Title
    alertDialog.setTitle("Confirm Delete...");

    // Setting Dialog Message
    alertDialog.setMessage("Delete from History?");



    // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
                         //whatever you want to do
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
                         //cancel the dialog
            dialog.cancel();
        }
    });


    alertDialog.show();
}