平时网上看到别人写的多是弹出一些基本控件。由于项目中用到的是弹出一个listView,所以就拿它来举例吧!
由于使用Dialog建立的对话框必须要有标题,如果不调用它的setTitle()则标题区域会变成空白。所以如果不希望有标题哪怕是空白标题,就选择AlertDialog来创建自定义的对话框吧!
上代码:
一:首先,写一个布局文件。就一个按钮。并且设置它的监听事件:这个布局文件就略了。在它的监听事件里面声明一个方法,actionAlertDialog()
btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { actionAlertDialog(); } });
二:其次,写第二个布局文件。这个文件就是要弹出的界面布局文件,命名为:myview.xml。如下:注意注意!给root命名了id:layout_myview.这个以后要用到!
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_myview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/mylistview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:cacheColorHint="#ffffff" android:divider="#ffffff" android:dividerHeight="1dip" android:listSelector="#00000000" > </ListView> </LinearLayout>
listview item项如下:命名为:item_listview
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:orientation="horizontal" > <ImageView android:id="@+id/item_pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#003399" android:paddingRight="10dp" android:text="小宝" /> <TextView android:id="@+id/item_birth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#003399" android:paddingRight="10dp" android:text="2012-8-23" /> <Button android:id="@+id/item_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点我" /> </LinearLayout>
三:给自定义的ListView写一个适配器,命名为MyAdapter,注意,列表里的按钮监听事件是加到了这里哦~
/** @author Miya,2012-8-23,上午11:26:41,MyAlertDialog */ package com.example.myalertdialog; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MyAdapter extends BaseAdapter { Context context; ArrayList<Person> list; private LayoutInflater inflater; public MyAdapter(Context context,ArrayList list){ this.context = context; this.list = list; inflater = LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Person getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { Holder holder; if(convertView==null){ holder = new Holder(); convertView = inflater.inflate(R.layout.item_listview, null); holder.name = (TextView) convertView.findViewById(R.id.item_name); holder.birth = (TextView) convertView.findViewById(R.id.item_birth); holder.itembtn = (Button) convertView.findViewById(R.id.item_btn); convertView.setTag(holder); }else{ holder = (Holder) convertView.getTag(); } holder.name.setText(list.get(position).name); holder.birth.setText(list.get(position).birth); holder.itembtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, list.get(position).name+"的生日是:"+list.get(position).birth, Toast.LENGTH_LONG).show(); } }); return convertView; } protected class Holder{ TextView name; TextView birth; Button itembtn; } }
四:万事俱备,只欠东风!
我们要在actionAlertDialog()方法里面,实现我们的弹出效果。
protected void actionAlertDialog(){ ArrayList<Person> list = initData(); AlertDialog.Builder builder; AlertDialog alertDialog; // Context context = getApplicationContext(); LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.myview, (ViewGroup)findViewById(R.id.layout_myview)); ListView myListView = (ListView) layout.findViewById(R.id.mylistview); MyAdapter adapter = new MyAdapter(context, list); myListView.setAdapter(adapter); builder = new AlertDialog.Builder(context); builder.setView(layout); alertDialog = builder.create(); alertDialog.show(); } protected ArrayList<Person> initData(){ ArrayList<Person> list = new ArrayList<Person>(); Person p; for(int i=0;i<10;i++){ p = new Person(); p.name = "小宝"+i; p.birth = "2012-8-23"; list.add(p); } return list; }
来看下效果吧!(丑了些,因为偶的主要目的是为了弹出自定义listView)
//////////////////////////////////////////////////////////////////////////