Android中的AlertDialog和ProgressDialog用法
手机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用法的更多相关文章
- Android 中的AlertDialog使用自定义布局
Android使用指定的View开发弹窗功能 Android开发中进程会使用到我们的AlertDialog,但是比较可惜的是我们的Android原生的AlertDialog的效果又比较的简陋,这个时候 ...
- Android中的Handler的具体用法
Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.Android利用Handler来实现UI线程的更新的. Handler是Android中的消息发送器,其在哪个Activit ...
- Android中Parcelable与Serializable接口用法
转自: Android中Parcelable接口用法 1. Parcelable接口 Interface for classes whose instances can be written to a ...
- Android中Parcelable和Serializable接口用法
1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...
- Android中Context的总结及其用法
在android中我们经常遇到这样的情况,在创建一个对象的时候往往需要传递一个this参数,比如:语句 MyView mView = new MyView(this),要求传递一个this参数,这个t ...
- Android中的AlertDialog使用示例五(自定义对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例四(多项选择确定对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例三(单向选择确定对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例二(普通选项对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
随机推荐
- Java web之jsp,xml(2020.1.7)
1.xml文档规则 xml声明 字符集 xml元素的基本规则: 合法标签名 嵌套子元素 空元素
- 5314跳跃游戏IV
题目:给你一个整数数组 arr ,你一开始在数组的第一个元素处(下标为 0).每一步,你可以从下标 i 跳到下标: i + 1 满足:i + 1 < arr.length i - 1 ...
- 学习spring第二天
Spring第二天笔记 1. 使用注解配置Spring入门 1.1. 说在前面 学习基于注解的IoC配置,大家脑海里首先得有一个认知,即注解配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦 ...
- 三、NOSQL之Memcached缓存服务实战精讲第二部
1.Memcached服务安装 Memcached的安装比较简单,很多平台都是支持Memcached,常见的有:Linux .Windows 服务端端: cd /home ...
- 黑马eesy_15 Vue:04.综合案例(前端Vue实现)
黑马eesy_15 Vue:02.常用语法 黑马eesy_15 Vue:03.生命周期 黑马eesy_15 Vue:04.Vue案例(ssm环境搭建) 黑马eesy_15 Vue:04.综合案例(前端 ...
- Macroevolution|Silent changes|CNEs|Transposable elements|Neutral sites
Interspecies genomic comparison 因为脊椎动物诞生早,在演化过程中有Macroevolution(因为自然选择或遗传漂变导致持续突变同时表型发生改变),但是存在一种基因缺 ...
- python运算符的优先级顺序
最近开始学习python,听大家说python很强大,可以作为脚本语言,支持面向对象.面向过程编程,兼具编译性和解释性的一门动态语言.作为一名程序员有必要掌握这一门强大的"胶水语言" ...
- 字典 -> model
1.使用KVC init(dict : [String : Any]) { super.init() setValuesForKeys(dict) } override func setValue(_ ...
- idea常用快捷键(对于新手不建议切换使用eclipse)
查看方法实现:ctrl+alt+鼠标实现父类方法:ctrl+i查看方法的具体实现:ctrl+alt(鼠标再点击方法)快速导包:alt+enter格式化:Ctrl+Alt+L格式化当前行:ctrl+sh ...
- Qt HWND的句柄与QWidget的转换
QT中用到HWND的句柄在编程中遇到了问题,第三方API用了hwnd类型做形参,但是QT中又没有该类型,可以做如下操作来解决问题. 在.h中先声明: HWND m_hWnd; 再声明 public: ...