欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android入门之AlertDialog用法实例分析

程序员文章站 2024-01-23 11:22:52
本文实例讲述的是alertdialog,这种对话框会经常遇到。alertdialog跟win32开发中的dialog不一样,alertdialog是非阻塞的,而阻塞的对话框...

本文实例讲述的是alertdialog,这种对话框会经常遇到。alertdialog跟win32开发中的dialog不一样,alertdialog是非阻塞的,而阻塞的对话框用的是popupwindow。

先贴出该程序运行的截图:

Android入门之AlertDialog用法实例分析

main.xml的源码:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >

<button android:id="@+id/button01" android:layout_height="wrap_content" android:text="非layout型对话框" android:layout_width="fill_parent"></button>
<button android:id="@+id/button02" android:layout_height="wrap_content" android:text="layout型对话框" android:layout_width="fill_parent"></button><view android:id="@+id/view01" android:layout_width="wrap_content" android:layout_height="wrap_content"></view>

</linearlayout>

下图是非layout型对话框,直接使用alertdialog

Android入门之AlertDialog用法实例分析

下图是使用了layout的对话框,可以自定义控件,实现更复杂的对话框

Android入门之AlertDialog用法实例分析

dialoglayout.xml的源码:

<?xml version="1.0" encoding="utf-8"?>

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:orientation="vertical">
 <edittext android: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>

程序源码:

package com.testalertdialog;

import android.app.activity;
import android.app.alertdialog;
import android.content.context;
import android.content.dialoginterface;
import android.os.bundle;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.popupwindow;


public class testalertdialog extends activity {
 button btnshowdialog;
 button btnshowdialog_layout;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    //定义按钮
    btnshowdialog=(button)this.findviewbyid(r.id.button01);
    btnshowdialog.setonclicklistener(new clickevent());
    btnshowdialog_layout=(button)this.findviewbyid(r.id.button02);
    btnshowdialog_layout.setonclicklistener(new clickevent());
  }
  
  
  //统一处理按键事件
  class clickevent implements onclicklistener{

   @override
   public void onclick(view v) {
   // todo auto-generated method stub
   if(v==btnshowdialog)
    showdialog(testalertdialog.this);
    
   else if(v==btnshowdialog_layout)
    showdialog_layout(testalertdialog.this);
   
   }

  }

  //显示基本的alertdialog
 private void showdialog(context context) {
 alertdialog.builder builder = new alertdialog.builder(context);
 builder.seticon(r.drawable.icon);
 builder.settitle("title");
 builder.setmessage("message");
 builder.setpositivebutton("button1",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle("点击了对话框上的button1");
   }
  });
 builder.setneutralbutton("button2",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle("点击了对话框上的button2");
   }
  });
 builder.setnegativebutton("button3",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle("点击了对话框上的button3");
   }
  });
 builder.show();
 }


  //显示基于layout的alertdialog
 private void showdialog_layout(context context) {
 layoutinflater inflater = layoutinflater.from(this);
 final view textentryview = inflater.inflate(
  r.layout.dialoglayout, null);
 final edittext edtinput=(edittext)textentryview.findviewbyid(r.id.edtinput);
 final alertdialog.builder builder = new alertdialog.builder(context);
 builder.setcancelable(false);
 builder.seticon(r.drawable.icon);
 builder.settitle("title");
 builder.setview(textentryview);
 builder.setpositivebutton("确认",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle(edtinput.gettext());
   }
  });
 builder.setnegativebutton("取消",
  new dialoginterface.onclicklistener() {
   public void onclick(dialoginterface dialog, int whichbutton) {
   settitle("");
   }
  });
 builder.show();
 }
}