Android入门之AlertDialog用法实例分析_Android

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

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

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

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

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();
 }
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, alertdialog
, 入门
用法
alertdialog用法、alertdialog、android alertdialog、alertdialog.builder、alertdialog dismiss,以便于您获取更多的相关知识。

时间: 2024-09-15 02:13:03

Android入门之AlertDialog用法实例分析_Android的相关文章

Android入门之AlertDialog用法实例分析

本文实例讲述的是AlertDialog,这种对话框会经常遇到.AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的是PopupWindow. 先贴出该程序运行的截图: main.xml的源码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.co

Android中AlertDialog用法实例分析_Android

本文实例分析了Android中AlertDialog用法,分享给大家供大家参考,具体如下: Android中AlertDialog为一些程序提供了对话框,有些功能能够进一步满足程序的需要.下面举例介绍. 程序如下: import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.

Android开发中PopupWindow用法实例分析_Android

本文实例分析了Android开发中PopupWindow用法.分享给大家供大家参考,具体如下: private TextView tv_appmanager_title; private ListView lv_app_manager; private LinearLayout ll_appmanager_loading; private AppManagerProvider provider; private List<AppManagerInfo> infos ; private AppM

Android开发之Location用法实例分析_Android

本文实例讲述了Android开发中Location用法.分享给大家供大家参考,具体如下: Location 在Android 开发中还是经常用到的,如通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发)等.而在Android 中通过LocationManager来获取Location .通常获取Location 有GPS 获取,WIFI 获取. 这边介绍一个简单的小Demo ,来教大家如何获取Location ,从而获取经纬度. 第一步:创

Android入门之Gallery用法实例解析_Android

本文实例介绍的Android的Gallery控件是个很不错的看图控件,可以大大减轻开发者对于看图功能的开发,并且效果也很美观.本文实例中的Gallery的用法,主要实现用反射机制来动态读取资源中的图片. 该实例代码运行的效果图如下:   main.xml源码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.andro

Android内容提供者ContentProvider用法实例分析_Android

本文实例讲述了Android内容提供者ContentProvider用法.分享给大家供大家参考,具体如下: PersonContentProvider内容提供者类 package com.ljq.db; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher;

Android开发之Sqliteopenhelper用法实例分析_Android

本文实例讲述了Android开发之Sqliteopenhelper用法.分享给大家供大家参考.具体分析如下: 如果在安卓开发中,直接使用Activity中的openOrCreateDatabase(name, mode, factory)会有一系列跟随的问题.比如说数据库升级.更新等. 最好是使用其封装版本:SQLiteOpenHelper 继承和扩展SQLiteOpenHelper类主要做的工作就是重写以下两个 方法. onCreate(SQLiteDatabase db) : 当数据库被首次

Android Service控件用法实例分析_Android

本文实例讲述了Android Service控件用法.分享给大家供大家参考,具体如下: 1.Service是一个应用程序的组件 2.Service没有图形化界面 3.用来处理耗时比较长的功能(下载.播放MP3) 4.更新ContentProvider.Intent以及系统的启动 Servcie不是一个单独的进程,不是一个线程 定义一个Service比较简单,只要继承Service类,实现其生命周期的方法即可.一个定义好的Service必须在AndroidManifest.xml文件中通过<ser

Android开发之BroadcastReceiver用法实例分析_Android

本文实例讲述了Android开发中BroadcastReceiver用法.分享给大家供大家参考.具体分析如下: 在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制. Braodcast Receiver顾名思义就是广播接收器,它和事件处理机制类似,但是事件处理机制是程序组件级别的(比如:按钮的单击事件),而广播事件处理机制是系统级别的.我们可以用Intent来启动一个组件,也可以用sendBroadcast()方法发起一个系统级别的事件广播来传递消息