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实现方法为建造者模式. ...
随机推荐
- 01 语言基础+高级:1-8 File类与IO流_day10【缓冲流、转换流、序列化流】
day10[缓冲流.转换流.序列化流] 主要内容 缓冲流 转换流 序列化流 打印流 教学目标 能够使用字节缓冲流读取数据到程序 能够使用字节缓冲流写出数据到文件 能够明确字符缓冲流的作用和基本用法 能 ...
- Salt常用命令二
############################模块############################ 查看模块列表——module salt ‘minion’ sys.list_mo ...
- 常见的nosql数据库有哪些?以及他们的特点与区别?
一.常见的nosql 二.Redis,Memcache,MongoDb的特点 (1).Redis 优点: 1.支持多种数据结构,如 string(字符串). list(双向链表).dict(hash表 ...
- ZJNU 2206 - 染色
开纵横两个结构体数组,记录连续涂了一整行或者一整列的情况 再开一个map,记录涂点 #include<iostream> #include<algorithm> #includ ...
- 微信小程序使用第三方FontIcon库的部分字体图标
一.提取部分图标重新制作TTF字库 我没有使用网上大多数文章写的淘宝提供的fonticon,而只使用了Ionicons的几个图标,所以打开Ionicons的官网点击右上角的Designer pack下 ...
- P2P平台疯狂爆雷后,你的生活受到影响了吗?
最近这段时间P2P爆雷的新闻和报道一直占据着各大财经和科技媒体的重要位置.而据网贷之家数据显示,截至2018年7月底,P2P网贷行业累计平台数量达到6385家(含停业及问题平台),其中问题平台累计为2 ...
- python实现图书管理系统
# 用户注册 def logon(): print("欢迎来到图书管理系统注册页面~") username = input("请输入用户名:") if len( ...
- JavaScript面试题(珍爱生命,远离面试)
1.使用 typeof bar === "object" 判断 bar 是不是一个对象有神马潜在的弊端?如何避免这种弊端? 使用 typeof 的弊端是显而易见的(这种弊端同使用 ...
- QLIKVIEW-SALESORDER\DELIVERYNOTICE\OUTSTOCK\INVOICE
//销售订单SALESORDER_TMP:NoConcatenateLOAD T_SAL_ORDER.LE_ID, [T_SAL_ORDER.LCY CODE], T_SAL_ORDER.SYSTEM ...
- tf.boolean_mask
tf.boolean_mask 的作用是 通过布尔值 过滤元素 def boolean_mask(tensor, mask, name="boolean_mask", axis=N ...