广告
返回顶部
首页 > 资讯 > 移动开发 >Android入门之AlertDialog用法实例分析
  • 403
分享到

Android入门之AlertDialog用法实例分析

alertdialogAndroid 2022-06-06 10:06:44 403人浏览 泡泡鱼
摘要

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

本文实例讲述的是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;
  
  @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各种对话框的用法实例详解Android使用自定义alertdialog实现确认退出按钮Android中AlertDialog的六种创建方式Android AlertDialog自定义样式实现代码Android中阻止AlertDialog关闭实例代码Android仿iOS自定义AlertDialog提示框Android AlertDialog对话框用法示例Android使用AlertDialog实现的信息列表单选、多选对话框功能Android编程实现Dialog窗体监听的方法Android开发实现AlertDialog中View的控件设置监听功能分析


--结束END--

本文标题: Android入门之AlertDialog用法实例分析

本文链接: https://www.lsjlt.com/news/27005.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android入门之AlertDialog用法实例分析
    本文实例讲述的是AlertDialog,这种对话框会经常遇到。AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的...
    99+
    2022-06-06
    alertdialog Android
  • Android中AlertDialog用法实例分析
    本文实例分析了Android中AlertDialog用法,分享给大家供大家参考,具体如下: Android中AlertDialog为一些程序提供了对话框,有些功能能够进一步满足...
    99+
    2022-06-06
    alertdialog Android
  • Android AlertDialog实例分析
    这篇文章主要讲解了“Android AlertDialog实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android AlertDialog实例分析”吧!AlertDialog可以...
    99+
    2023-06-25
  • Android入门之PopupWindow用法实例解析
    本文实例介绍一下PopupWindow对话框。PopupWindow是阻塞对话框,只有在外部线程 或者 PopupWindow本身做退出操作才可以执行。PopupWindow完...
    99+
    2022-06-06
    popupwindow Android
  • Android入门之Gallery用法实例解析
    本文实例介绍的Android的Gallery控件是个很不错的看图控件,可以大大减轻开发者对于看图功能的开发,并且效果也很美观。本文实例中的Gallery的用法,主要实现用反射机...
    99+
    2022-06-06
    gallery Android
  • Android入门之RelativeLayout、FrameLayout用法分析
    本文讲述的是Android中RelativeLayout、FrameLayout的用法。具体如下: RelativeLayout是一个按照相对位置排列的布局,跟Absolute...
    99+
    2022-06-06
    relativelayout framelayout Android
  • Android入门之Gallery+ImageSwitcher用法实例解析
    继上一篇介绍了如何使用Gallery控件之后,本文就来讲一下Gallery 与ImageSwitcher的结合使用。本文所述实例代码将实现一个简单的浏览图片的功能。 先贴出程序...
    99+
    2022-06-06
    gallery Android
  • Android入门之Style与Theme用法实例解析
    就目前的互联网发展来看,已经有越来越多互联网企业都在Android平台上部署其客户端,并且为了提升用户体验,这些客户端都做得布局合理而且美观。本文所要介绍的Android的St...
    99+
    2022-06-06
    Android
  • Android开发入门之Appwidget用法分析
    本文实例讲述了Android Appwidget用法。分享给大家供大家参考,具体如下: App Widgets 是一个小型应用程序的View  他可以嵌入到其他应用程...
    99+
    2022-06-06
    android开发 Android
  • Android开发入门之Notification用法分析
    本文实例讲述了Android中Notification用法。分享给大家供大家参考,具体如下: Notification可以理解为通知的意思一般用来显示广播信息 用Notific...
    99+
    2022-06-06
    android开发 notification Android
  • Android开发入门之Service用法分析
    本文实例讲述了Android中Service用法。分享给大家供大家参考,具体如下: 关于Service的讲解网上已经很多了,这里是关于自己通过写代码Service的一点体会 还...
    99+
    2022-06-06
    service android开发 Android
  • Android入门之TabHost与TabWidget实例解析
    本文实例介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用。Android的Tab控件...
    99+
    2022-06-06
    tabhost Android
  • Android入门之LinearLayout、AbsoluteLayout的用法实例讲解
    本文实例介绍了Android中LinearLayout、AbsoluteLayout的用法,希望能对于初学Android的朋友起到一点帮助作用。具体内容如下: Android ...
    99+
    2022-06-06
    Android
  • HTML入门实例分析
    这篇文章主要介绍“HTML入门实例分析”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“HTML入门实例分析”文章能帮助大家解决问题。 HTML是英文Hyper Te...
    99+
    2022-10-19
  • SpringMVC入门实例分析
    今天小编给大家分享一下SpringMVC入门实例分析的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解...
    99+
    2022-10-19
  • Elasticsearch入门实例分析
    这篇文章主要介绍“Elasticsearch入门实例分析”,在日常操作中,相信很多人在Elasticsearch入门实例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Elasticsearch入门实例分析...
    99+
    2023-06-05
  • Python入门之基础语法的示例分析
    这篇文章将为大家详细讲解有关Python入门之基础语法的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。python有哪些常用库python常用的库:1.requesuts;2.scrapy;3.p...
    99+
    2023-06-15
  • Android控件之ProgressBar用法实例分析
    本文实例讲述了Android控件之ProgressBar用法。分享给大家供大家参考。具体如下: ProgressBar位于android.widget包下,其继承于View,主...
    99+
    2022-06-06
    progressbar Android
  • Android控件之GridView用法实例分析
    本文实例讲述了Android控件之GridView用法。分享给大家供大家参考。具体如下: GridView是一项显示二维的viewgroup,可滚动的网格。一般用来显示多张图片...
    99+
    2022-06-06
    gridview Android
  • Android控件之ScrollView用法实例分析
    本文实例讲述了Android控件之ScrollView用法。分享给大家供大家参考。具体如下: ScrollView滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动跳来显...
    99+
    2022-06-06
    scrollview Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作