手机APP对话框是很多APP都有的下面来看下怎么实现的吧,

打开Android studio 然他自动创建好布局和类;

下面我们修改activity_main.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"
android:layout_marginTop="60pt"
android:textAllCaps="false"
/> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button"
android:layout_marginTop="3pt"
android:textAllCaps="false"
/> </LinearLayout>

  再activity_main.xml添加了两个按钮 分别是用于 AlertDialog  和 ProgressDialog的

  下面我们再看ActivityMain中的代码

  

package com.example.administrator.myappalertdialog;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mBtn1;
private Button mBtn2; //声明控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionbar = getSupportActionBar(); // 去除标题栏
if(actionbar!= null)
{
actionbar.hide();
}
mBtn1 = (Button) findViewById(R.id.btn1); //查找控件
mBtn1.setOnClickListener(this); //创建点击事件
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn2.setOnClickListener(this);
} public void onClick(View view) //点击事件
{
switch(view.getId())
{
case R.id.btn1:AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is AlertDialog"); //对话框标题
dialog.setMessage("Hello world!");  //内容
dialog.setCancelable(true);    //可撤销性
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"OK",Toast.LENGTH_SHORT).show(); //提示内容
} //确定按钮的点击事件
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show(); //提示内容
}//取消按钮的点击事件
});
dialog.show(); //全部显示出来
break;
//下面的原理一样。
case R.id.btn2:
ProgressDialog progressdialog = new ProgressDialog(MainActivity.this);
progressdialog.setTitle("This is ProgressDialog");
progressdialog .setMessage("Mr.Jiang");
progressdialog.setCancelable(false);
progressdialog.show();
break; default: Toast.makeText(MainActivity.this,"ERROR",Toast.LENGTH_SHORT).show();break;
}
}
}

  上面ProgressDialog 没有确定和取消按钮,我们可以把progressdialog.setCancelable里面的值改成true,按下BACK键即可退出,还有可以用progressdialog.dismiss();来去取消关闭对话框;

    下面附上第一个和第二个效果图.

Android中的AlertDialog和ProgressDialog用法的更多相关文章

  1. Android 中的AlertDialog使用自定义布局

    Android使用指定的View开发弹窗功能 Android开发中进程会使用到我们的AlertDialog,但是比较可惜的是我们的Android原生的AlertDialog的效果又比较的简陋,这个时候 ...

  2. Android中的Handler的具体用法

    Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.Android利用Handler来实现UI线程的更新的. Handler是Android中的消息发送器,其在哪个Activit ...

  3. Android中Parcelable与Serializable接口用法

    转自: Android中Parcelable接口用法 1. Parcelable接口 Interface for classes whose instances can be written to a ...

  4. Android中Parcelable和Serializable接口用法

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  5. Android中Context的总结及其用法

    在android中我们经常遇到这样的情况,在创建一个对象的时候往往需要传递一个this参数,比如:语句 MyView mView = new MyView(this),要求传递一个this参数,这个t ...

  6. Android中的AlertDialog使用示例五(自定义对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  7. Android中的AlertDialog使用示例四(多项选择确定对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  8. Android中的AlertDialog使用示例三(单向选择确定对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  9. Android中的AlertDialog使用示例二(普通选项对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

随机推荐

  1. 关于vue内只要html元素的代码

    使用v-model v-text v-html vue会解析出入的html代码报错 则上传sku的description时需要html页面的代码 所以在description 所在的表单元素内加入 v ...

  2. python画图例子代码

    matplotlib包,使得python可以使用类似matlab的命令 双坐标,子图例子 fig, axes = plt.subplots( 2,1, figsize=(14, 14) ) ax = ...

  3. Regex of Perl: ?= 指的是如果后面匹配到,则后面的内容不做替换,保留。 ab, s/a?=b/x/,变为xb,即后面匹配到b,b保留不做替换

    Regex of Perl: ?= 指的是如果后面匹配到,则后面的内容不做替换,保留.  ab, s/a?=b/x/,变为xb,即后面匹配到b,b保留不做替换

  4. shell_xtrabackup_backup_mysql

    #!/bin/bash# Name: xtra_back_mysql.sh# Desc:该脚本使用xtrabackup工具对mysql数据库进行增量备份,根据自己需求可以自行修改# Usage:./x ...

  5. 基础篇九:模块介绍(--with-http_stub_status_module)

    下面--with 即为编译安装的模块 下面我们来介绍--with-http_stub_status_module此模块 vim  /etc/nginx/conf.d/default.conf 然后检查 ...

  6. docker-compose up Windows named pipe error:(code: 2)

    执行docker-compose up启动项目时,报如下错误: ERRORERROR: Windows named pipe error: 膸碌脥艂艕艊藳禄碌藵脰赂露篓碌脛脦脛慕牛藝艁 (code: ...

  7. 吴裕雄--天生自然python学习笔记:python 用pygame模块制作一个音效播放器

    用 Sound 对象制作一个音效播放器. 应用程序总览 程序在执行后默认会把 WAV 音频文件加载到清单中,单击“播放”按钮可开始 播放,同时显示 “正在播放 xxx 音效”的信息 . 播放过程中,可 ...

  8. 系统学习Javaweb5----JavaScript1

    注意:java和JavaScript没有半毛钱关系!!! 说明:过年歇着歇着不知不觉就歇了七天,嘿嘿,从今天开始继续学习. 学习笔记: 1.JavaScript概述. 1.1.JavaScript是什 ...

  9. Python常用的数据结构详解

    数据结构:通俗点说,就是储存大量数据的容器.这里主要介绍Python的4种基本数据结构:列表.字典.元组.集合. 格式如下: 列表:list = [val1,val2,val3,val4],用中括号: ...

  10. [Linux] Ubuntu 配置nfs

    安装NFS Server: 1. 执行命令 "$ sudo apt-get install nfs-kernel-server",安装nfs server 端 2. 创建需要用来分 ...