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. Apache和PHP环境配置

    最近闲来想学习一下PHP. 工欲善其事,必先利其器.我的PHP环境配置了三遍,才安装成功. 下面就分享一下我的安装经验. 1.Apache2.4,PHP5.6,MySql5.6这些都是从官网下载的. ...

  2. ecshop获取商品销量函数

    以下函数会获取订单状态为已完成的订单中该商品的销量,此函数放在lib_goods.php文件中即可调用 /** * 获取商品销量 * * @access      public * @param    ...

  3. JAVA基础-JSON

    /** * 1.根据接收到的JSON字符串来解析字符串中所包含的数据和数据对象 */ System.out.println("1.根据接收到的JSON字符串来解析字符串中所包含的数据和数据对 ...

  4. word和.txt文件转html 及pdf文件, 使用poi jsoup itext心得

    word和.txt文件转html 及pdf文件, 使用poi jsoup  itext心得本人第一次写博客,有上面不足的或者需要改正的希望大家指出来,一起学习交流讨论.由于在项目中遇到了这一个问题,在 ...

  5. Example007关闭窗口时关闭父窗口

    <!--实例007关闭窗口时刷新父窗口--> <!DOCTYPE html> <html lang="en"> <head> < ...

  6. 用php+mysql+ajax实现淘宝客服或阿里旺旺聊天功能 之 后台页面

    在上一篇随笔中,我们已经看了如何实现前台的对话功能:前台我限定了店主只有一人,店铺只有一个,所有比较单一,但后台就不一样了,而后台更像是我们常见的聊天软件:当然,前台也应该实现这种效果,但原理懂了,可 ...

  7. 你猜这个题输出啥?-- java基础概念

    最近在看java编程思想,大部分内容都觉得没啥意思,但是突然看到一个基本概念(似乎都忘了),于是写了测试题,我想这辈子也不会忘这个概念了. 题目如下: public class Suber exten ...

  8. 计算机浏览器存储技术cookie、sessionStorage、localStorage

    HTTP无状态协议是指协议对于事务处理没有记忆能力.会话跟踪协议的状态是指下一次传输可以"记住"这次传输信息的能力,无状态是指同一个会话(注意什么叫同一个会话)的连续两个请求互相不 ...

  9. [图形学] Chp17 OpenGL光照和表面绘制函数

    这章学了基本光照模型,物体的显示受到以下效果影响:全局环境光,点光源(环境光漫反射分量,点光源漫反射分量,点光源镜面反射分量),材质系数(漫反射系数,镜面反射系数),自身发光,雾气效果等.其中点光源有 ...

  10. rownumber和rowid伪劣用法

    select rownum ,deptno,dname loc from  dept; select deptno,dname,loc from dept where rownum=1; select ...