androidUI第二部分---1.2.2Activity对Dialog的管理及Dialog的自定义布局
来源:互联网 发布:js input不可编辑状态 编辑:程序博客网 时间:2024/05/23 13:03
前面提到过Activity对dialog的管理有两个回调函数onCreateDialog(int) 和onPrepareDialog(int, Dialog) ,使用步骤:首先需要给不同的Dialog定义不同的id号,通过onCreateDialog(int)方法传入id号参数来初始化生成不同的dialog,显示不通的dialog时则可以直接调用Activity的showDialog(int)方法根据id号来显示不同的dialog.
Dialog也可以自定义布局,在上面加上一些textview,button等控件,步骤很简单只需要自己定义一个新的xml布局文件,然后生成dialog时,掉用builder的setview()方法指定新的view,当然view和xml之间的转化可以通过下面代码完成:
LayoutInflater li = LayoutInflater.from(this);
View v= li.inflate(R.layout.mypatterndialog, null);
示例代码如下:
public class UIDemo extends Activity implements OnClickListener{
Dialog dialog;
public final int ALERT_DIALOG = 1; //第一步
public final int MYPATTERN_DIALOG = 2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button)this.findViewById(R.string.button1);
button1.setOnClickListener(this );
}
protected Dialog onCreateDialog(int id) {//重写此方法
switch(id)
{
case ALERT_DIALOG:
dialog= createDialog();//根据不同id生成不同dialog
break;
case MYPATTERN_DIALOG:
dialog = createMyDialog();
break;
}
return dialog;
}
protected void onPrepareDialog(int id, Dialog dialog)
{
}
public Dialog createDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("hellohow");
builder.setPositiveButton(R.string.hello,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("dialog");
}
});
builder.setNegativeButton(R.string.button2,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("ca");
}
});
return builder.create();
}
public Dialog createMyDialog()
{
LayoutInflater li = LayoutInflater.from(this);
View v= li.inflate(R.layout.mypatterndialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("mypattern");
builder.setView(v);
builder.setPositiveButton(R.string.button3,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("button3 click");
}
});
return builder.create();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
this.showDialog(MYPATTERN_DIALOG);//根据id显示不同的dialog
Log.v("111", "pressed");
}
}
- androidUI第二部分---1.2.2Activity对Dialog的管理及Dialog的自定义布局
- Activity对Dialog的管理
- androidUI第二部分---1.2.1Dialog,AlertDialog如何生成
- 自定义布局的dialog
- 自定义dialog的布局样式
- 自定义dialog的布局样式
- Android自定义dialog的布局
- 自定义dialog的布局样式
- Dialog自定义布局的实现
- 自定义布局Dialog 自定义dialog右上角带叉号的dialog 点击右上角叉号关闭dialog
- 用Activity的dialog主题实现自定义dialog
- Android之自定义activity的Dialog
- Activity和自定义Dialog的区别
- 自定义圆角风格dialog的Activity
- 使用自定义透明Dialog样式的Activity
- 使用自定义透明Dialog样式的Activity
- 使用自定义透明Dialog样式的Activity
- 使用自定义透明Dialog样式的Activity
- 递归调用,遍历CTreeCtrl的树形结构
- Oracle 10G compare MS SQL SERVER 2008
- STL(Standard Template Library)适配容器
- 基于Mysql的分页显示
- TempData may not works under distributed system
- androidUI第二部分---1.2.2Activity对Dialog的管理及Dialog的自定义布局
- Coooool URL
- daaaa
- 程序员:下一次面试前你需要准备的五个基本步骤
- 第一篇就自己写吧
- flex 组件生命周期
- Channel,Band
- 接触群智能算法
- epoll和select/poll的区别