Dialog中Gallery的使用方法

來源:互聯網
上載者:User

Dialog 有兩種使用方法:

一:在Activity中重寫onCreateDialog(int id)的方法來建立Dialog的對象並返回,然後調用Activity中的showDialog(int id)和DismissDialog(int id)來顯示和隱藏Dialog對話方塊

@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("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubstr=0;}});}return dialog.create();}

用上面這種方法建立時,注意如果要建立Button的click事件的話,必須建立Builder對象,讓後用Builder對象去建立Button,然後返回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() {@Overridepublic 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();}

注意我剛開始建立的是Builder對象builder  直到builder.setView(textEntryView).creat()  調用了creat()才建立了Dialog的對象,然後調用show()方法顯示Dialog對話方塊

上面建立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() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {str = ia.Name(arg2);}});  

先找到控制項gallery ,然後將資料放進gallery適配器中。

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;}@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];}}

具體Gallery的各種用法可以參考部落格:Android中的對話方塊AlertDialog提示合集.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.