Dialog的4中功能的展示,以及他的监听方法
2011-10-18 09:13
323 查看
public class ActivityMain extends Activity {
//为dialog排序方便后面判断使用
private static final int DIALOG1 = 1;
private static final int DIALOG2 = 2;
private static final int DIALOG4 = 4;
private static final int DIALOG3 = 3;
//判断dialog,并却返回到指定的方法中,这里的方法是以个回调方法,执行后面的showDialog()方法后都会回调
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG1:
return buildDialog1(ActivityMain.this);
case DIALOG2:
return buildDialog2(ActivityMain.this);
case DIALOG3:
return buildDialog3(ActivityMain.this);
case DIALOG4:
return buildDialog4(ActivityMain.this);
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog){
if(id==DIALOG1){
setTitle("测试");
}
}
//为Activity设置布局以及按钮的监听
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG1);
}
});
Button buttons2 = (Button) findViewById(R.id.buttons2);
buttons2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG2);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG3);
}
});
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG4);
}
});
}
//为Dialog设置内容以及按键监听
private Dialog buildDialog1(Context context) {
//AlertDialog是Dialog的子类,Builder为AlertDialog的内部类,通过Builder为AlertDialog来设置内容
AlertDialog.Builder builder = new AlertDialog.Builder(context);
//builder设置AlertDialog中的提示图标
builder.setIcon(R.drawable.alert_dialog_icon);
//builder设置AlertDialog中的提示文本
builder.setTitle(R.string.alert_dialog_two_buttons_title);
//builder设置AlertDialog中左按键的名以及按键监听
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
//builder设置AlertDialog中右按键的名以及按键监听
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
//返回 builder的create()方法
return builder.create();
}
private Dialog buildDialog2(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_two_buttons_msg);
//builder设置AlertDialog中的多文本提示信息
builder.setMessage(R.string.alert_dialog_two_buttons2_msg);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
//builder设置AlertDialog中中间按键以及监听
builder.setNeutralButton(R.string.alert_dialog_something,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的进入详细按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
private Dialog buildDialog3(Context context) {
//LayoutInflater为动态加载xml方法主要为不是activity的其他空间添加布局方法
LayoutInflater inflater = LayoutInflater.from(this);
//创建View来赋值LayoutInflater的布局
final View textEntryView = inflater.inflate(
R.layout.alert_dialog_text_entry, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_text_entry);
//通过builder为LayoutInflater加载布局
builder.setView(textEntryView);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
//下面是进度对话框
private Dialog buildDialog4(Context context) {
//申明进度对话框,可以直接new.
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("正在下载歌曲");
dialog.setMessage("请稍候……");
return dialog;
}
}
//为dialog排序方便后面判断使用
private static final int DIALOG1 = 1;
private static final int DIALOG2 = 2;
private static final int DIALOG4 = 4;
private static final int DIALOG3 = 3;
//判断dialog,并却返回到指定的方法中,这里的方法是以个回调方法,执行后面的showDialog()方法后都会回调
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG1:
return buildDialog1(ActivityMain.this);
case DIALOG2:
return buildDialog2(ActivityMain.this);
case DIALOG3:
return buildDialog3(ActivityMain.this);
case DIALOG4:
return buildDialog4(ActivityMain.this);
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog){
if(id==DIALOG1){
setTitle("测试");
}
}
//为Activity设置布局以及按钮的监听
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG1);
}
});
Button buttons2 = (Button) findViewById(R.id.buttons2);
buttons2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG2);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG3);
}
});
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG4);
}
});
}
//为Dialog设置内容以及按键监听
private Dialog buildDialog1(Context context) {
//AlertDialog是Dialog的子类,Builder为AlertDialog的内部类,通过Builder为AlertDialog来设置内容
AlertDialog.Builder builder = new AlertDialog.Builder(context);
//builder设置AlertDialog中的提示图标
builder.setIcon(R.drawable.alert_dialog_icon);
//builder设置AlertDialog中的提示文本
builder.setTitle(R.string.alert_dialog_two_buttons_title);
//builder设置AlertDialog中左按键的名以及按键监听
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
//builder设置AlertDialog中右按键的名以及按键监听
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
//返回 builder的create()方法
return builder.create();
}
private Dialog buildDialog2(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_two_buttons_msg);
//builder设置AlertDialog中的多文本提示信息
builder.setMessage(R.string.alert_dialog_two_buttons2_msg);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
//builder设置AlertDialog中中间按键以及监听
builder.setNeutralButton(R.string.alert_dialog_something,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的进入详细按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
private Dialog buildDialog3(Context context) {
//LayoutInflater为动态加载xml方法主要为不是activity的其他空间添加布局方法
LayoutInflater inflater = LayoutInflater.from(this);
//创建View来赋值LayoutInflater的布局
final View textEntryView = inflater.inflate(
R.layout.alert_dialog_text_entry, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_text_entry);
//通过builder为LayoutInflater加载布局
builder.setView(textEntryView);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
//下面是进度对话框
private Dialog buildDialog4(Context context) {
//申明进度对话框,可以直接new.
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("正在下载歌曲");
dialog.setMessage("请稍候……");
return dialog;
}
}
相关文章推荐
- UISearchDisplayController(iOS < 8.0)实例应用实现搜索功能和VC展示效果, 以及代理方法的介绍
- 回退键的监听以及相关的Dialog使用方法
- ListView用法、对ListView监听的三种方法以及ListView中Adapter的使用方法 2014-10-09 0 个评论
- 安装SQL Server 2012过程中出现“启用windows功能NetFx3时出错”(错误原因、详细分析及解决方法)以及在Windows Server2012上安装.NET Framework 3
- Android开发系列(十四):ListView用法、对ListView监听的三种方法以及ListView中Adapter的使用方法
- 自适应软键盘的Dialog以及监听软键盘弹起
- 弹出dialog 窗口; 以及在dialog 中关闭并且刷新父窗体的方法
- iOS 在viewController中监听Home键触发以及重新进入界面的方法
- C语言中strtok使用方法与原理,以及自实现函数功能
- 利用videoView创建播放器,实现展示视频列表和搜索sd卡功能 以及android6.0的动态授权等功能
- DialogFragment 监听按键事件的方法(onkeydown)
- 如何使用JFileChooser 的showSaveDialog()方法--实现保存文件功能?(完整)
- dwz选择带回功能dialog不能翻页解决方法
- 饼图的展示以及获取单元格数据的方法
- 自适应软键盘的Dialog以及监听软键盘弹起
- solr5使用方法篇以及高级功能facet使用
- 继承的概述以及使用方法代码展示以及继承中构造方法的使用
- 【转】bootbox自定义dialog、confirm、alert样式,以及基本设置方法setDefaults中可用参数
- 自适应软键盘的Dialog以及监听软键盘弹起
- 自适应软键盘的Dialog以及监听软键盘弹起