首页 > 代码库 > 自定义上下文对话框

自定义上下文对话框

以下是一种方法,还有一种方法:http://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html

public
class MyContextDialog extends Dialog { public MyContextDialog(Context context) { super(context); } public MyContextDialog(Context context, int theme) { super(context, theme); } public static class Builder { private Context context; private String title; private String[] contextItem;private View contentView; private AdapterView.OnItemClickListener itemListener; public Builder(Context context) { this.context = context; } public Builder setTitle(int title) { this.title = (String) context.getText(title); return this; } public Builder setTitle(String title) { this.title = title; return this; } public Builder setItems(String[] contextItem, AdapterView.OnItemClickListener listener) { this.contextItem = contextItem; this.itemListener = listener; return this; } public Builder setContentView(View v) { this.contentView = v; return this; } public MyContextDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final MyContextDialog dialog = new MyContextDialog(context, R.style.MyDialogs); View layout = inflater.inflate(R.layout.dialog_context, null); dialog.addContentView(layout, new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); ((TextView) layout.findViewById(R.id.dialog_tv_title)) .setText(title); ListView lv = (ListView) layout .findViewById(R.id.dialog_context_list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.dialog_listitem, contextItem); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long ids) { itemListener.onItemClick(parent, view, position, ids); dialog.dismiss(); } }); dialog.setContentView(layout); return dialog; } } }