问题描述
- 用选择文件和相机的内容显示一个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; }
那么如何添加这两个选项卡呢?
解决方案
使用下面的代码,可以提供两个选项卡:
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;
}
}
解决方案二:
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()
时间: 2024-11-02 10:09:19