2 markdowneide markdowneide 于 2013.06.06 14:46 提问

用选择文件和相机的内容显示一个dialog box

我想打开一个 dialog box,然后给出两个选项:
1- 从SD Card中选择文件 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;
                    }

那么如何添加这两个选项卡呢?

2个回答

curie_87
curie_87   2013.06.06 17:44
已采纳

使用下面的代码,可以提供两个选项卡:

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

Fuction 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;
            }
        }
cj6585256
cj6585256   2013.06.06 15:14
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
View view = LayoutInflart.from(ctx).inflart(R.layout.yourLayout);
        builder.setView(view);

下面步骤就一样了

yourLayout.xml中可以写2个TextView 也可以写ListView ,LisetView就得设置Adapter,设置OnItemClickListerner()

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!