Android Studio - 如何关闭对话框

时间:2016-03-02 13:10:50

标签: android android-studio

只是想知道在选择一个选项时如何关闭对话框。

这是代码

public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    if (item.getItemId() == R.id.launch_voip_call) {
        Utils.startCall(this, contact);
        return true;
    } 
    else if(item.getItemId() == R.id.launch_attachment){

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setCancelable(true);

        View choiceView = getLayoutInflater().inflate(R.layout.dialog_image_source_chooser, null);

        ImageButton cameraButton = (ImageButton) choiceView.findViewById(R.id.cameraButton);

        ImageButton galleryButton = (ImageButton) choiceView.findViewById(R.id.galleryButton);

        cameraButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent cameraIntent = new Intent(SingleConversationActivity.this, CameraActivity.class);
                cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid());
                startActivity(cameraIntent);

            }
        });

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(YourActivity.this);

                LayoutInflater inflater = YourActivity.this.getLayoutInflater();
                View dialogView = inflater.inflate(R.layout.dialog_image_source_chooser, null);
                dialogBuilder.setView(dialogView);

               ImageButton cameraButton = (ImageButton) dialogView.findViewById(R.id.cameraButton);

    ImageButton galleryButton = (ImageButton) dialogView.findViewById(R.id.galleryButton);


                final AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.show();

                cameraButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                         alertDialog.dismiss();
                         Intent cameraIntent = new Intent(SingleConversationActivity.this, CameraActivity.class);
            cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid());
            startActivity(cameraIntent);

                    }
                });

答案 2 :(得分:0)

alertDialog.dismiss()方法中使用onClick()

另外,要显示对话框alertDialog.show()

答案 3 :(得分:0)

请参阅this answer

尝试添加dialogBuilder.dismiss();