【问题标题】:AlertDialog after clicking on a button单击按钮后的 AlertDialog
【发布时间】:2025-12-30 03:15:12
【问题描述】:

我想在单击一个按钮(参见下文)后添加一个 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 可见?

谢谢。

【问题讨论】:

  • 还有什么问题?
  • 如何让我的 AlertDialog 可见?

标签: android button android-intent android-alertdialog


【解决方案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


        }
    }

}

【讨论】:

  • 这似乎是解决方案,但永远不会调用 onActivityResult
  • 另一边目标activity的onActivityResult被调用
【解决方案2】:
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();

【讨论】:

    【解决方案3】:

    你可以这样做

    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();
    }
    

    【讨论】: