显示包含选择文件和相机内容的对话框

时间:2012-02-14 07:46:59

标签: java android eclipse android-layout

我想打开一个对话框,它给了我两个选项。

1-从SD卡中选择文件 2-从相机拍摄快照

现在我正在使用此代码

 receipt.setOnClickListener(new View.OnClickListener() 
         {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(RECEIPT_DIALOG_ID);
            }
        });
protected Dialog onCreateDialog(int id) {
            Dialog dialog = null;
            AlertDialog.Builder builder = new Builder(this);
 case RECEIPT_DIALOG_ID:
                    builder.setTitle("Choose your file");
                        dialog = builder.create();
                      return dialog;
                    }

现在,我该如何添加这两个选项。

最好的问候

3 个答案:

答案 0 :(得分:1)

试试这个,它提供了两个选项

final CharSequence[] items = {"Camera", "Memory Card"};

builder.setTitle(R.string.pic_option);
                    builder.setIcon(R.drawable.camera_icon);
                    builder.setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {
                          launchCamera(item);
                        }
                    });
                    builder.create();
                    builder.show();

和函数launchCamera(item)在这里

public void launchCamera(int id){

            switch (id) {
            case 0:
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                ((Activity)getParent()).startActivityForResult(cameraIntent, 1888);
                break;
            case 1: 
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                ((Activity)getParent()).startActivityForResult(intent, 2);
                break;
            default:
                break;
            }
        }

答案 1 :(得分:0)

直接在您的方法中使用它。它将在对话框中显示2个按钮。

    Dialog dialog;
    dialog = new Dialog(getParent());
    dialog.setContentView(R.layout.camdialog);
    dialog.setTitle("         Select the Image");
    dialog.setCancelable(true);
    sdCard = (Button) dialog.findViewById(R.id.sdCard);
    camDialog = (Button) dialog.findViewById(R.id.camDialog);
    dialog.show();
    dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, 200);

答案 2 :(得分:0)

试试这个......

            new AlertDialog.Builder(menu)
                .setTitle("Choose your file")
                .setMessage("Your msg")
                .setPositiveButton("Choose file from SD Card",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                    // Your code
                            }
                        })
                .setNegativeButton("Take a snapshot from Camera",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                //Your code
                            }
                        }).show();