There are two ways to use dialog:
1. Override the oncreatedialog (int id) method in the activity to create the dialog object and return it. Then, call showdialog (int id) and dismissdialog (int id) in the activity) to display and hide the dialog box
@ Overrideprotected 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 () {@ overridepublic void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {STR = IA. name (arg2) ;}}); dialog = new alertdialog. builder (this ). setview (LL); dialog. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {}}); dialog. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stubstr = 0 ;}});} return Diener. create ();}
When using the above method to create a button, note that if you want to create a button click event, you must create a builder object, and then use the builder object to create a button, and then return dialog. Create ();
Then, call showdialog (100); In the oncreate () method.
2. directly create a dialog object dialog and call the show () and dismiss () methods.
Public void check () {alertdialog. 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. setonitemclicklist Ener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {STR = IA. name (arg2) ;}}); builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int whichbutton) {}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int whichbutton) {STR = 0 ;}}); builder. setview (textentryview ). create (). show ();}
Note that the builder object builder is created at the beginning until builder. setview (textentryview ). creat () creates the dialog object only after calling creat (), and then calls the show () method to display the dialog box.
I used different methods to create a view object.
Final view textentryview = layoutinflater. From (this). Inflate (R. layout. picturelayout, null );
Linearlayout LL = (linearlayout) layoutinflater. From (this). Inflate (R. layout. picturelayout, null );
You can use either of them, but the first method is recommended, because the first method is more common and sometimes your layout may not be linear.
Gallery usage:
Call the code in your call method or oncreate () method:
ImageAdapter ia = new ImageAdapter(this); gallery = (Gallery)textEntryView.findViewById(R.id.gallery1);gallery.setAdapter(ia);gallery.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {str = ia.Name(arg2);}});
First find the control Gallery, and then put the data into the gallery adapter.
The imageadapter Page code is as follows:
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;}@Overridepublic int getCount() {return myImageIds.length;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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];}}
For details about the usage of gallery, refer to blog: alertdialog In the android dialog box.