2015-09-22 45 views
0

我正在嘗試修改對話框的佈局,然後執行一些功能並在我的功能結束時關閉警告框。從另一個函數關閉自定義對話框

佈局文件

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

我在顯示對話框和定義功能兩幅圖像上的點擊...我想關閉該對話框,儘快執行相應的功能。

我試過使用解僱,但它不工作..!

+0

使用myAlertDialog.dismiss時會出現什麼錯誤? –

回答

0

替代,我發現......使用建設者然後分配到一個對話框,使用解僱函數首先創建一個警告對話框,

更新 -

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份額!

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> 
+0

解僱不工作! – abhi9393

+0

它工作,如果你在runOnUiThread()嘗試它嘗試它。 –

+0

我試過了......只要我使用解僱功能,它就會顯示錯誤! – abhi9393