安卓 : how Custom Dialog returns intent result to parent activity

标签 android android-intent customdialog onactivityresult

在我的 mainactivity 类中,单击一个按钮后,它会显示一个对话框,在单击其中任何一个按钮后显示“拍摄照片”、“拍摄视频”、从图库中选择按钮它必须执行相应的操作并将文件路径返回给 mainactivity。 在 mainactivity 中使用 startActivityForResult&onActivityResult 很容易做到。 但是我如何在自定义对话框中使用 intent with 并将自定义对话框的 intent 结果返回到 mainactivity

感谢您的宝贵时间。

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

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(){
        //do action
        String filePath = data.getDataString();
        filename.setText(filePath);
    }
}

最佳答案

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

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

关于安卓 : how Custom Dialog returns intent result to parent activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17934257/

相关文章:

android - gradle如何知道哪个项目在运行

android - Volley : [355] NetworkDispatcher. processRequest : Unhandled exception java. lang.IllegalArgumentException

android - 如何将 Html 文本共享到 whatsapp Intent

android - 在 onClick 中获取 Dialog 的引用

android - 自定义对话框高度以匹配内容

java - 如何创建两行ListView

android - 单击按钮时从 Android 应用程序发送电子邮件

android - Activity 重叠内容之间的过渡

android - 动画在自定义对话框中不起作用

Android:处理 onClickListener 时出错