时隔一年,又要准备做Android的开发了,最近复习和整理一下Android的知识。这次要说的是AlertDialog,这种对话框会经常遇到。AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的是PopupWindow。
先贴出程序运行的截图:
![](http://hi.csdn.net/attachment/201010/21/0_1287627861tFJn.gif)
main.xml的源码:
-
<?xmlversion="1.0"encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
-
<Buttonandroid:id="@+id/Button01"android:layout_height="wrap_content"android:text="非Layout型对话框"android:layout_width="fill_parent"></Button>
-
<Buttonandroid:id="@+id/Button02"android:layout_height="wrap_content"android:text="Layout型对话框"android:layout_width="fill_parent"></Button><Viewandroid:id="@+id/View01"android:layout_width="wrap_content"android:layout_height="wrap_content"></View>
-
-
</LinearLayout>
下图是非Layout型对话框,直接使用AlertDialog
![](http://hi.csdn.net/attachment/201010/21/0_1287627867ZXik.gif)
下图是使用了Layout的对话框,可以自定义控件,实现更复杂的对话框
![](http://hi.csdn.net/attachment/201010/21/0_1287627872giWf.gif)
dialoglayout.xml的源码:
-
<?xmlversion="1.0"encoding="utf-8"?>
-
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"android:layout_height="wrap_content"
-
android:orientation="vertical">
-
<EditTextandroid:layout_height="wrap_content"
-
android:layout_width="fill_parent"android:layout_marginLeft="20dip"
-
android:layout_marginRight="20dip"android:textAppearance="?android:attr/textAppearanceMedium"android:id="@+id/edtInput"/>
-
</LinearLayout>
程序源码:
-
packagecom.testAlertDialog;
-
-
importandroid.app.Activity;
-
importandroid.app.AlertDialog;
-
importandroid.content.Context;
-
importandroid.content.DialogInterface;
-
importandroid.os.Bundle;
-
importandroid.view.Gravity;
-
importandroid.view.LayoutInflater;
-
importandroid.view.View;
-
importandroid.view.View.OnClickListener;
-
importandroid.widget.Button;
-
importandroid.widget.EditText;
-
importandroid.widget.PopupWindow;
-
-
-
publicclasstestAlertDialogextendsActivity{
-
ButtonbtnShowDialog;
-
ButtonbtnShowDialog_Layout;
-
-
@Override
-
publicvoidonCreate(BundlesavedInstanceState){
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
btnShowDialog=(Button)this.findViewById(R.id.Button01);
-
btnShowDialog.setOnClickListener(newClickEvent());
-
btnShowDialog_Layout=(Button)this.findViewById(R.id.Button02);
-
btnShowDialog_Layout.setOnClickListener(newClickEvent());
-
}
-
-
-
-
classClickEventimplementsOnClickListener{
-
-
@Override
-
publicvoidonClick(Viewv){
-
-
if(v==btnShowDialog)
-
showDialog(testAlertDialog.this);
-
-
elseif(v==btnShowDialog_Layout)
-
showDialog_Layout(testAlertDialog.this);
-
-
}
-
-
}
-
-
-
privatevoidshowDialog(Contextcontext){
-
AlertDialog.Builderbuilder=newAlertDialog.Builder(context);
-
builder.setIcon(R.drawable.icon);
-
builder.setTitle("Title");
-
builder.setMessage("Message");
-
builder.setPositiveButton("Button1",
-
newDialogInterface.OnClickListener(){
-
publicvoidonClick(DialogInterfacedialog,intwhichButton){
-
setTitle("点击了对话框上的Button1");
-
}
-
});
-
builder.setNeutralButton("Button2",
-
newDialogInterface.OnClickListener(){
-
publicvoidonClick(DialogInterfacedialog,intwhichButton){
-
setTitle("点击了对话框上的Button2");
-
}
-
});
-
builder.setNegativeButton("Button3",
-
newDialogInterface.OnClickListener(){
-
publicvoidonClick(DialogInterfacedialog,intwhichButton){
-
setTitle("点击了对话框上的Button3");
-
}
-
});
-
builder.show();
-
}
-
-
-
-
privatevoidshowDialog_Layout(Contextcontext){
-
LayoutInflaterinflater=LayoutInflater.from(this);
-
finalViewtextEntryView=inflater.inflate(
-
R.layout.dialoglayout,null);
-
finalEditTextedtInput=(EditText)textEntryView.findViewById(R.id.edtInput);
-
finalAlertDialog.Builderbuilder=newAlertDialog.Builder(context);
-
builder.setCancelable(false);
-
builder.setIcon(R.drawable.icon);
-
builder.setTitle("Title");
-
builder.setView(textEntryView);
-
builder.setPositiveButton("确认",
-
newDialogInterface.OnClickListener(){
-
publicvoidonClick(DialogInterfacedialog,intwhichButton){
-
setTitle(edtInput.getText());
-
}
-
});
-
builder.setNegativeButton("取消",
-
newDialogInterface.OnClickListener(){
-
publicvoidonClick(DialogInterfacedialog,intwhichButton){
-
setTitle("");
-
}
-
});
-
builder.show();
-
}
-
}
分享到:
相关推荐
Android入门第九篇之AlertDialog.docx
时隔一年,又要准备做Android的开发了,最近复习和整理一下Android的知识。这次要说的是AlertDialog,这种对话框会经常遇到。AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的是...
Android中实现Iphone样式的AlertDialog.rar
在Android中实现Iphone样式的AlertDialog; 使用Dialog实现。
Android之AlertDialog源码
Android之修改AlertDialog对话框及使用系统Holo风格
Android高级应用源码-Android中实现Iphone样式的AlertDialog.zip
Android中实现Iphone样式的AlertDialog源码,是一个很不错的Android源码,有兴趣的伙伴们抽时间可以看一下把。
025_android UI组件之 AlertDialog 的各种实现,很细致的视频教程,这个可以学哦。。
Android应用源码之Android中实现Iphone样式的AlertDialog.zip
026_android UI组件之 AlertDialog的自定义布局,很细致的视频教程,这个可以学哦。。
Android应用源码之Android中实现Iphone样式的AlertDialog.rar
android AlertDialog使用例程 一个在安卓平台上开发使用AlertDialog的例子 很实用很全哦
Android应用源码之Android中实现Iphone样式的AlertDialog_实现
android自定义AlertDialog,例如支付页面,先弹出自定义AlertDialog询问是否愿意支付,确定则弹出另外一个自定义AlertDialog,选择支付方式,android自定义AlertDialog,android自定义AlertDialog,android自定义...
android AlertDialog对话框的类型集合 ,比较全面的讲述了 AlertDialog 的 种类 , Demo 的综合性比较强 , Demo 可直接运行
android AlertDialog 单选 多选 输入框 疯狂android讲义
Android AlertDialog对话框 自定义实现遮罩层
* 各种功能实现弹窗(自定义弹窗位置,添加动画效果) * 1.设置AlertDialog弹窗并且2秒后自动关闭(自定义关闭AlertDialog) * 2.设置popuwindow弹窗并且2秒后自动关闭 * 3.弹出后自带震动提示
安卓Android源码——alertDialog.rar