Android对话框

在一个例子中展示四种对话框。

设置四个按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bt_dialog1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="普通对话框" />

    <Button
        android:id="@+id/bt_dialog2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选对话框" />

    <Button
        android:id="@+id/bt_dialog3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选对话框" />

    <Button
        android:id="@+id/bt_dialog4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="进度条对话框" />

</LinearLayout>

分别是普通对话框、单选对话框、多选对话框、进度条对话框

package com.example.stylethemetest;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;

        Button btDialog1 = (Button) findViewById(R.id.bt_dialog1);
        Button btDialog2 = (Button) findViewById(R.id.bt_dialog2);
        Button btDialog3 = (Button) findViewById(R.id.bt_dialog3);
        Button btDialog4 = (Button) findViewById(R.id.bt_dialog4);

        btDialog1.setOnClickListener(this);
        btDialog2.setOnClickListener(this);
        btDialog3.setOnClickListener(this);
        btDialog4.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 1. 普通对话框
            case R.id.bt_dialog1:
                AlertDialog.Builder alertDialog1 = new AlertDialog.Builder(mContext);

                alertDialog1.setTitle("注意");
                alertDialog1.setMessage("真的要删除吗?");
                alertDialog1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(mContext, "你点击了确定", Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(mContext, "你点击了取消", Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog1.show();
                break;
            // 2. 单选对话框
            case R.id.bt_dialog2:
                final AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(mContext);

                alertDialog2.setTitle("选择学历");
                final String[] edu = {"小学", "初中", "高中", "本科", "研究生", "博士", "其他"};
                // 选择默认选中,-1表示不选中
                alertDialog2.setSingleChoiceItems(edu, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String eduLevel = edu[which];
                        Toast.makeText(mContext, eduLevel+which, Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog2.setPositiveButton("选择", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO:处理确定逻辑
                    }
                });
                alertDialog2.show();
                break;
            // 3. 多选对话框
            case R.id.bt_dialog3:
                AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(mContext);
                alertDialog3.setTitle("选择你喜欢吃的水果");
                final String[] items = {"榴莲", "苹果", "葡萄", "香蕉", "黄瓜", "火龙果", "荔枝"};
                // bool值和上面的条目对应,true表示默认选中
                final boolean[] checkedItems = {false, true, false, true, false,
                        false, false};
                alertDialog3.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        // 下标和是否被选中
                        Toast.makeText(mContext, which+ " " +isChecked, Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog3.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        StringBuilder fruits = new StringBuilder();
                        for (int i = 0; i < items.length; i++) {
                            if (checkedItems[i]) {
                                // 就证明是选中的
                                String fruit = items[i];
                                fruits.append(fruit).append(" ");
                            }
                        }
                        Toast.makeText(mContext, fruits, Toast.LENGTH_SHORT).show();
                    }
                });
                // show里面调用了create
                alertDialog3.show();
                break;
            // 4. 进度条对话框
            case R.id.bt_dialog4:
                final ProgressDialog progressDialog = new ProgressDialog(mContext);
                // 默认STYLE_SPINNER,小圆圈选装。可选水平进度条
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setTitle("加载");
                progressDialog.setMessage("Loading...");
                // false表示用户可不能通过按下back键或者对话框外的地方退出,默认是true。
                progressDialog.setCancelable(true);
                progressDialog.show();
                // 进入条相关组件可以在子线程更新UI
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < 101; i++) {
                        // 与Thread.sleep不同的是,他不会抛出interruptedException;
                            SystemClock.sleep(50);
                            progressDialog.setProgress(i);
                        }
                        // cancel也调用了dismiss,不过还可可响应setOnCancelListener
                        // hide只是隐藏,没有dismiss掉
                        progressDialog.dismiss();
                    }
                }).start();

                break;
        }
    }
}
 

上图是普通对话框

上图是单选对话框

上图是多选对话框

上图是进度条对话框

Android帧动画初步认识

其实就是加载一系列的图片资源

  1. 首先放入一系列图片放入res/drawable中,我放入了drawable-xxhdpi。
  2. 在上述文件夹下新建一个xml,如下。 android:oneshot="false"表示循环一次后还接着循环。true的话就是循环一次停止在最后一帧。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >
    <item android:drawable="@drawable/a" android:duration="500" />
    <item android:drawable="@drawable/b" android:duration="500" />
    <item android:drawable="@drawable/c" android:duration="500" />
    <item android:drawable="@drawable/d" android:duration="500" />
    <item android:drawable="@drawable/e" android:duration="500" />
</animation-list>

主界面就放一个ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.animationtest.MainActivity">

    <ImageView
        android:id="@+id/iv_animation"
        android:layout_width="100dp"
        android:layout_height="100dp" />

</LinearLayout>
 

MainActivity

package com.example.animationtest;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView rocketImage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rocketImage = (ImageView) findViewById(R.id.iv_animation);
        // [1]找到ImageView控件 用来显示动画效果
        rocketImage = (ImageView) findViewById(R.id.iv_animation);
        // [2]设置背景资源, 传入的是xml
        rocketImage.setBackgroundResource(R.drawable.my_animation);
        // [3]获取AnimationDrawable 类型
        AnimationDrawable ad = (AnimationDrawable) rocketImage.getBackground();
        // [4]开始执行动画
        ad.start();
    }
}
 

结果如下图片所示,其实是动态的。截图成静态了。


by @sunhaiyu

2017.5.18

Android对话框和帧动画的更多相关文章

  1. Android简单逐帧动画Frame的实现(三)

    android之动画(三)通过AnimationDrawable控制逐帧动画     android与逐帧动画: 效果图: 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的状态. ...

  2. Android简单逐帧动画Frame的实现(二)

    Android简单逐帧动画Frame的实现   Android简单逐帧动画Frame的实现 1.逐帧动画 即是通过播放预先排序好的图片来实现动态的画面,感觉像是放电影. 2.实现步骤: 1. 在工程里 ...

  3. Android中的帧动画与补间动画的使用

    前言 在日常开发中,我们有时候须要一些好看的动画效果,这时能够充分利用Android提供的这几种动画来实现. Android提供了3种类型的动画: 补间动画:补间动画能够应用于View,让你能够定义一 ...

  4. Java乔晓松-android中的帧动画FrameByFrame

    先看效果后上代码: 动画开始---- 动画切换的界面---- 动画播放完毕后的跳转界面----- 重要的方法: imageView.setBackgroundResource(R.anim.frame ...

  5. Android动画系列之帧动画和补间动画

    原文首发于微信公众号:jzman-blog,欢迎关注交流! Android 提供三种动画:帧动画.补间动画和属性动画,本篇文章介绍帧动画以及补间动画的使用,属性动画的使用将在后面的文章中分享,那就来复 ...

  6. Android--逐帧动画FrameAnimation

    前言 开门见山,本篇博客讲解一下如何在Android平台下播放一个逐帧动画.逐帧动画在Android下可以通过代码和XML文件两种方式定义,本篇博客都将讲到,最后将以一个简单的Demo来演示两种方式定 ...

  7. Android基础笔记(十)- 帧动画、补间动画具体解释、对话框

    帧动画 补间动画Tween Animation 对话框以及面试中的注意点 帧动画 帧动画非常easy,我们首先看一下Google官方解释This is a traditional animation ...

  8. Android动画效果之Frame Animation(逐帧动画)

    前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...

  9. android 帧动画,补间动画,属性动画的简单总结

      帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...

随机推荐

  1. Why you should QC your reads AND your assembly?

    鲤鱼基因组:http://www.ntv.cn/a/20140923/52953.shtml   关于鲤鱼基因组的测定,数据质量控制遭到质疑. Why you should QC your reads ...

  2. Ztorg木马分析: 从Android root木马演变到短信吸血鬼

    本月第二次,Google 从官方应用商店 Google Play 中移除了伪装成合法程序的恶意应用.被移除的应用都属于名叫 Ztorg 的 Android 恶意程序家族.目前为止,发现的几十个新的Zt ...

  3. ue4加载界面(loadingscreen)的实现

    即使开放世界已然成为现今游戏趋势,切换关卡过程中的读条仍是很难避免的,譬如进入房屋.传送到其他世界等等. 于是就引入了loadingscreen这一需求. loadingscreen顾名思义就是加载过 ...

  4. 并发编程(一):从头到脚解读synchronized

    一.目录 1.多线程启动方式 2.synchronized的基本用法 3.深度解析synchronized 4.同步方法与非同步方法是否能同时调用? 5.同步锁是否可重入(可重入锁)? 6.异常是否会 ...

  5. win7双系统安装openSUSE13.2解决【引导加载器安装期间出错】问题

    原始日期:2015-08-17 14:16 昨晚不知道哪根筋不对,突然想装一个liunx系统,与win7形成双系统,最终选定openSUSE13.2,想想以前也安装过Ubuntu,应该差不多,所以直接 ...

  6. Linux网络服务12——NFS共享服务

    Linux网络服务12--NFS共享服务 一.NFS简介 端口号:TCP.UDP 111端口 NFS(Network File System)网络文件系统,是一种基于TCP/IP传输的网络文件系统协议 ...

  7. python flask(多对多表查询)

    我们在flask的学习中,会难免遇到多对多表的查询,今天我也遇到了这个问题.那么我想了好久.也没有想到一个解决的办法,试了几种方法,可能是思路的限制我放弃了,后来,我就在网上百度,可是发现百度出来的结 ...

  8. angularJS directive详解(自定义指令)

    Angularjs指令定义的API AngularJs的指令定义大致如下 其中return返回的对象包含很多参数,下面一一说明 1.restrict (字符串)可选参数,指明指令在DOM里面以什么形式 ...

  9. 【hexo】如何在一个小时内搭载个人博客

    版权申明:本文为博主原创文章,未经博主允许不得转载.如需转载,请私聊博主. 什么是hexo Hexo是一个开源的静态博客生成器,用node.js开发,作者是台湾大学生tommy351. 前期准备 安装 ...

  10. 【Android Developers Training】 82. 序言:传输数据时减少对电池寿命的影响

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...