1.对话框的使用

1.1AlertDialog的显示

简单对话框以及监听的设置:重点掌握三个按钮(也就是三上单词):

PositiveButton(确认按钮);NeutralButton(忽略按钮)

AlertDialog.Builder bud1=new Builder(mContext);

bud1.setTitle("提示信息");

bud1.setMessage("您的信息已提交完成!");

bud1.setPositiveButton("确认", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss(); //对话框消失

}

});

bud1.setPositiveButton(" 取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();//取消对话框 Toast.makeText(mContext, "你已取消会话!", Toast.LENGTH_LONG).show();

}

});

bud1.setNeutralButton("忽略", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(Code06_03.this, "你已忽略。。。", Toast.LENGTH_LONG).show();

}

});

//单选按钮,选取其中一个

//1.设置数据源

final String[] items = {"android", "ipone", "Symbian"};

//2.数据适配

bud1.setItems(items, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(mContext,items[which],Toast.LENGTH_SHORT).show();

}

});

//自定义对话框

ImageView img = new ImageView(this);

img.setImageResource(R.drawable.icon);

Bud1

.setView(img)

bud1.create().show();//显示对话框

1.2.ProgressDialog的显示

关键为什么在子线程中调用UI操作未报错问题,经过分析,

ProgressDialg内部已经实现了Hanlder,因而未报错。而runOnUiThread的使用也显的多余了。

protected void dialog9() {

new Thread(new Runnable() {

@Override

public void run() {

try {

Thread.sleep(5 * 1000);

// progressDialog.dismiss();

runOnUiThread(finishDialog);

} catch (InterruptedException e) {

}

}

}).start();

progressDialog = ProgressDialog.show(Code06_07.this, "请稍等",

"数据正在加载中...", true);

}

private Runnable finishDialog = new Runnable() {

@Override

public void run() {

progressDialog.dismiss();

}

};

m_pDialog = new ProgressDialog(Code06_09.this);

// 设置进度条风格,风格为长形

m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// 设置ProgressDialog 标题

m_pDialog.setTitle("提示");

// 设置ProgressDialog 提示信息

m_pDialog.setMessage("这是一个长形对话框进度条");

// 设置ProgressDialog 标题图标

m_pDialog.setIcon(R.drawable.icon);

// 设置ProgressDialog 进度条进度

m_pDialog.setProgress(100);

// 设置ProgressDialog 的进度条是否不明确

m_pDialog.setIndeterminate(false);

// 设置ProgressDialog 是否可以按退回按键取消

m_pDialog.setCancelable(true);

// 让ProgressDialog显示

m_pDialog.show();

new Thread() {

public void run() {

try {

while (m_count <= 100) {

// 由线程来控制进度。

m_pDialog.setProgress(m_count++);

Thread.sleep(1000);

}

m_pDialog.cancel();

} catch (InterruptedException e) {

m_pDialog.cancel();

}

}

}.start();

}

我在测试代码时,遇到一个过时的方法,有一个新的API以供使用

/***
 * 第一个参数是用来确定哪个按钮绑定监听
 *  @param whichButton Which button to set the listener on, can be one of
 *  有以下常量来提供选择
 *  BUTTON_POSITIVE
 *  {@link DialogInterface#BUTTON_POSITIVE}
 *  {@link DialogInterface#BUTTON_NEGATIVE}
 *  {@link DialogInterface#BUTTON_NEUTRAL
 **/
progress.setButton(AlertDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int i) {
        // 点击“确定按钮”取消对话框
        dialog.cancel();
    }
});

经以上分析,与代码的编写,可以总结出:ProgressDialog的延时,使用了Handler机制,可以放心的在Thread中延时,在Thread中的操作有 setProgress,dismiss等方法。

1.3 自定义对话框-

LayoutInflater inflater=mContext.getLayoutInflater();

mView=inflater.inflate(R.layout.dialog,// 自定义对话框视图

null);

AlertDialog.Builder(mContext).setView(mView);

飞哥今天讲的知识点,就是关于两个类的使用。ProgressDialog,AlertDialog这两个类,主要讲解了如何使用它们的方法,以及ProgressDialog在特性,在Thread中使用。

1.4AlertDialog解析

AlertDialog extens Dialog implents DialogInterface

三个子类:DataPicker,ProgressDialog,TimePickerDialog

接下来,看看APIGuida有什么惊喜呢?

AlertDialog

A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

通过这句话,我知道了飞哥的不易,把AlertDialog的功能全用了一遍,the,I  Knowed  it,and did it

DatePickerDialog or TimePickerDialog

A dialog with a pre-defined UI that allows the user to select a date or time.

一个提前定义好的对话框,允许用户选择时间或日期

you should use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object

看到这句话,不知道你有何感想-感觉前面的代码没用了有木有,大Boss是这个DialogFragment呀!不要急,请看后面。

简单的说:1.正确的处理Dialog的生命周期

2.对话框的简单利用,就像Fragment一样,当成一个组件

3.支持API级别泛围广Android1.6以上均支持,当然需要支持库

这里Google给出一个示例,封装AlertDialog,以复用

public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

当你获取实例后,便可以show了

参考:

FragmentDialog:详解:传送门

Andorid SDK Dialog

扩展阅读:

Full Screen DialogFragment (over ActionBar) in Android:传送门

Android学习笔记-Dialog详解的更多相关文章

  1. Angular6 学习笔记——路由详解

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  2. Angular6 学习笔记——组件详解之组件通讯

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  3. Angular6 学习笔记——组件详解之模板语法

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  4. JavaScript学习笔记-实例详解-类(二)

    实例详解-类(二)   //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...

  5. JavaScript学习笔记-实例详解-类(一)

    实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...

  6. Android学习——uses-sdk标签详解

    1 前言 我们都知道,Android的版本在不断的迭代,并且每个版本都加入了不同的新特性.那么随着Android的用户量越来越多,Android的开发人员就必须熟悉Android各个版本的特性并且确保 ...

  7. Android学习之 Intent详解

    一. Intent 作用 Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯.比如说调用startActivity()来 ...

  8. android 开发 对话框Dialog详解

    转载请注明出处:红亮的专栏:http://blog.csdn.net/liang5630/article/details/44098899 Android中的对话框形式大致可分为五种:分别是一般对话框 ...

  9. android学习——GestureDetector.OnGestureListener 详解

    Android Touch Screen 与传统Click Touch Screen不同,会有一些手势(Gesture),例如Fling,Scroll等等.这些Gesture会使用户体验大大提升.An ...

随机推荐

  1. Difference between Pragma and Cache-control headers?

    Pragma is the HTTP/1.0 implementation and cache-control is the HTTP/1.1 implementation of the same c ...

  2. django-based blog- mezzanine

    django-based blog- mezzanine zinnia 博客 hydra  暴力破解

  3. 51 nod 1006 最长公共子序列Lcs

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 参考博客 :http://blog.csdn.net/yysdsy ...

  4. Itext导出PDF,word,图片案例

    iText导出pdf.word.图片 一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生 ...

  5. Toast报错 android.content.res.Resources$NotFoundException

    Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_SHORT)                         ...

  6. 配置openerp的开发环境

    给Eclipse安装PyDev插件启用Eclipse————如果前面的步骤都正确无误的话,那么Eclipse就该能够正常启动了.第一次启动会让你选择一个工作空间,按缺省设置,勾选一下不再提醒,就可以了 ...

  7. C#多态;父类引用指向子类对象;new和override的区别;new、abstract、virtual、override,sealed关键字区别和使用代码示例;c#类的初始化顺序

    关于父类引用指向子类对象 例如: 有以下2个类 public class Father { public int age = 70; public static string name = " ...

  8. LeetCode Reverse Linked List II 反置链表2

    题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...

  9. android 自定义控件中获取属性的三种方式(转)

    第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值. (1)在xml文件中设置属性值 <com.example.activity.Ico ...

  10. Windows Store APP- C# to get IP Address

    using Windows.Networking.Connectivity; public String GetIPString() { String ipString = String.Empty; ...