时隔一年,又要准备做Android的开发了,最近复习和整理一下Android的知识。这次要说的是AlertDialog,这种对话框会经常遇到。AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的是PopupWindow。
先贴出程序运行的截图:
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
下图是使用了Layout的对话框,可以自定义控件,实现更复杂的对话框
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高级应用源码-Android中实现Iphone样式的AlertDialog.zip
Android应用源码之Android中实现Iphone样式的AlertDialog.zip
Android应用源码之Android中实现Iphone样式的AlertDialog.rar
常用的Builder对话框设置,包括只显示文本、带控件、自定义空、Builder间嵌套等
Android中实现Iphone样式的AlertDialog.zip项目安卓应用源码下载Android中实现Iphone样式的AlertDialog.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
源码参考,欢迎下载
安卓开发-Android中实现Iphone样式的AlertDialog.zip.zip
安卓Android源码——安卓Android中实现Iphone样式的AlertDialog.rar
安卓Android源码——安卓Android中实现Iphone样式的AlertDialog.zip
Android应用源码之alertDialog.zip项目安卓应用源码下载Android应用源码之alertDialog.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
Android中实现Iphone样式的AlertDialog.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
安卓Android源码——alertDialog.rar
AlertDialog.Builder多种不同用法。
安卓Android源码——中实现Iphone样式的AlertDialog.zip
Android之AlertDialog源码
https://blog.csdn.net/tnxlcn/article/details/82317490的工程文件打包
免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...