一:在Activity中重写onCreateDialog(int id)的方法来创建Dialog的对象并返回,然后调用Activity中的showDialog(int id)和DismissDialog(int id)来显示和隐藏Dialog对话框
@Override protected Dialog onCreateDialog(int id) { AlertDialog.Builder dialog=null; if(id==100){ LinearLayout ll = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.picturelayout, null); gallery = (Gallery)ll.findViewById(R.id.gallery1); gallery.setAdapter(ia); gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { str = ia.Name(arg2); } }); dialog = new AlertDialog.Builder(this).setView(ll); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub str=0; } }); } return dialog.create(); }
然后在oncreate()方法中调用showDialog(100);就行。
二:直接new一个Dialog对象dialog 然后调用show()和dismiss()方法
public void check(){ AlertDialog.Builder builder = new AlertDialog.Builder(SavePictureActivity.this); final View textEntryView = LayoutInflater.from(this).inflate(R.layout.picturelayout, null); // LinearLayout ll = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.picturelayout, null); gallery = (Gallery)textEntryView.findViewById(R.id.gallery1); gallery.setAdapter(ia); gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { str = ia.Name(arg2); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { str=0; } }); builder.setView(textEntryView).create().show(); }
上面创建View对象时我用了不同的方法
final View textEntryView = LayoutInflater.from(this).inflate(R.layout.picturelayout, null);
LinearLayout ll = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.picturelayout, null);
两种都可以,但是推荐用第一种方法,因为第一种方法更大众一些,有时候你的布局可能不是线性布局。
Gallery的用法:
在你的调用方法或oncreate()方法中调用一下代码就行:
ImageAdapter ia = new ImageAdapter(this); gallery = (Gallery)textEntryView.findViewById(R.id.gallery1); gallery.setAdapter(ia); gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { str = ia.Name(arg2); } });
ImageAdapter的页面代码如下:
public class ImageAdapter extends BaseAdapter{ Context context; public Integer[] myImageIds = {R.drawable.auto1, R.drawable.auto2,R.drawable.auto3,R.drawable.auto4, R.drawable.auto5,R.drawable.bike,R.drawable.bike1}; public ImageAdapter(Context context){ this.context = context; } @Override public int getCount() { return myImageIds.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView iv = new ImageView(context); iv.setImageResource(myImageIds[position]); iv.setScaleType(ImageView.ScaleType.FIT_XY); iv.setLayoutParams(new Gallery.LayoutParams(136, 88)); iv.setBackgroundColor(Color.WHITE); return iv; } public int Name(int id){ return myImageIds[id]; } }