2013-07-29 98 views
2

在我的主要活动类中,单击按钮时,它会显示一个对话框,其中显示“捕获照片”,“捕获视频”,从图库中选择“按钮这些按钮中的任何一个都必须执行相应的操作并返回文件的路径为mainactivity。 在主活动中使用startActivityForResult & onActivityResult可以很容易。 但是,如何在自定义对话框中使用intent并将意图结果从定制dilaog到mainactivity。Android:Custom Dialog如何将意图结果返回给父活动

感谢您的时间。

   takeaPhoto.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
         startActivityForResult(intent, ACTION_TAKE_PHOTO); 
        } 
       }); 

公共无效onActivityResult(INT requestCode,INT发送resultCode,意图数据){

开关(){

//做动作

字符串文件路径= data.getDataString();

filename.setText(filePath); }}

回答

1

这并不难。只需使用警报对话框,并确保您的观点是正确的。

final Context context = this; 
static final int SELECT_PICTURE = 1; 

takeaphoto.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v){ 

LayoutInflater myLayout = LayoutInflater.from(context); 
final View dialogView = myLayout.inflate(R.layout.YOURCUSTOM.XML, null); 
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 
alertDialogBuilder.setView(dialogView); 
final AlertDialog alertDialog = alertDialogBuilder.create();   


Button button1 = (Button) dialogView.findViewById(R.id.button1); 
button1.setOnClickListener(new OnClickListener(){ 
public void onClick(View v) { 


Intent intent = new Intent(Intent.ACTION_PICK); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(intent, SELECT_PICTURE); 
}}); 

alertDialog.show(); 
return;}}); 
+0

非常感谢:-) !.它为我工作。 – ss45

相关问题