从另一个功能关闭自定义对话框

时间:2015-09-22 11:45:44

标签: android dialog customdialog

我正在尝试修改对话框的布局,然后执行某些功能并在我的功能结束时关闭警告框。

布局文件

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:orientation="horizontal"
            android:layout_below="@+id/header"
            android:paddingTop="15dp"
            android:paddingBottom="15dp">
        <ImageView
            android:id="@+id/cam"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:paddingLeft="10dp"
            android:src="@drawable/ic_cam"
            android:layout_alignParentLeft="true"
            android:layout_weight="1"
            android:onClick="camera_listener"
            />

        <ImageView
            android:id="@+id/gal"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:paddingLeft="10dp"
            android:src="@drawable/ic_gal"
            android:layout_alignParentLeft="true"
            android:layout_weight="1"
            android:onClick="gallery_listener"
            />
        </LinearLayout>

Java文件

    AlertDialog.Builder myAlertDialog;    // Variable declared as a class member

    private void startDialog() {

            LayoutInflater inflater = this.getLayoutInflater();
            final View view = inflater.inflate(R.layout.activity_fab, null);
            myAlertDialog = new AlertDialog.Builder(this);
            myAlertDialog.show();
    }

public void gallery_listener(View view) {

        pictureActionIntent = new Intent(Intent.ACTION_PICK, null);
        pictureActionIntent.setType("image/*");
        pictureActionIntent.putExtra("return-data", true);
        startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
        myAlertDialog.setOnDismissListener();
    }

    public void camera_listener(View view) {

        pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
        pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        startActivityForResult(pictureActionIntent, CAMERA_REQUEST);
    }

我在对话框中显示两个图像并在点击时定义功能...我想在执行相应功能后立即关闭DIALOG。

我尝试使用解雇,但它无法正常工作..!

2 个答案:

答案 0 :(得分:0)

在第二种方法中尝试这个,

Main.java中的

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class DialogCustom extends Activity {

    AlertDialog myAlertDialog; 
    ImageView ivOne,ivTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_custom);

        ivOne=(ImageView)findViewById(R.id.cam);
        ivTwo=(ImageView)findViewById(R.id.gal);
        myAlertDialog = new AlertDialog.Builder(this).create();
        ivOne.setOnClickListener(new OnClickListener() {

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

        ivTwo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismis();
                    }
                });
            }
        });

    }

    private void startDialog() {
        myAlertDialog.show();
}
    public void dismis()
    {
        myAlertDialog.dismiss();
    }
}

并在main.xml文件中。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_below="@+id/header"
    android:orientation="horizontal"
    android:paddingBottom="15dp"
    android:paddingTop="15dp" >

    <ImageView
        android:id="@+id/cam"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_weight="1"
        android:onClick="camera_listener"
        android:padding="10dp"
        android:paddingLeft="10dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/gal"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_weight="1"
        android:onClick="gallery_listener"
        android:padding="10dp"
        android:paddingLeft="10dp"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

答案 1 :(得分:0)

Alternate i found ...首先使用构建器创建一个警告对话框,然后将其分配给一个对话框,并使用其中的dismiss函数

更新 -

private AlertDialog dialog; // Variable with class scope
private void startDialog() {

    LayoutInflater inflater = this.getLayoutInflater();
    final View view = inflater.inflate(R.layout.activity_fab, null);
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setView(view);

    dialog = builder.create();
    dialog.show();
    }

    public void gallery_listener(View view) {

        pictureActionIntent = new Intent(Intent.ACTION_PICK, null);
        pictureActionIntent.setType("image/*");
        pictureActionIntent.putExtra("return-data", true);
        startActivityForResult(pictureActionIntent, GALLERY_PICTURE);
        dialog.dismiss();
    }

这对我有用......如果有更好的方法PLZ分享!