Android学习笔记-Dialog详解

摘要:
“,tooth.LNGTH_LONG).show();}});bud1.setNeutralButton(“忽略”,newDialogInterface.OnClickListener(){@OverridepublicvotionClick(DialogInterfacedialog,int which){tooth.makeText(Code06_03.this,“您忽略了…”,tooth.ENGTH_LANG).shhow();}})//选择单选按钮之一//1.设置数据源finalString[]项={“android”,“ipone”,“Symbian”}//2.数据适配bud1.setItems//自定义对话框ImageViewimg=newImageView;img中。setImageResource;Bud1.setViewbud1.create()。show();//显示对话框1.2.ProgressDialog的关键是为什么在子线程中调用UI操作时没有错误报告。经过分析,ProgressDiag在内部实现了Hander,因此没有错误报告。runOnUiThread的使用也是多余的。

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);

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

1.4AlertDialog解析

AlertDialog extens Dialog implents DialogInterface

三个子类:DataPickerProgressDialogTimePickerDialog

接下来,看看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详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Ansible之路——第四章:Host InventoryHow to debug Bluetooth下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Bootstrap实现弹出框和提示框效果代码

一、Bootstrap弹出框使用过JQuery UI应该知道,它里面有一个dialog的弹出框组件,功能也很丰富。与jQuery UI的dialog类似,Bootstrap里面也内置了弹出框组件。打开bootstrap 文档可以看到它的dialog是直接嵌入到bootstrap.js和bootstrap.css里面的,也就是说,只要我们引入了bootstr...

Dialog的visible"陷阱"

  今天在调试代码时发现控制台经常输出一个error日志,出于好奇,于是探个究竟。若在你的Vue文件中有如下类似的示例代码: <template> <div> <p>Welcome to home</p>     <el-button @click="testButton">...

android dialog使用自定义布局 设置窗体大小位置

AlertDialog.Builder builder = new Builder(mContext); builder.setTitle("更新进度"); final LayoutInflater inflater = LayoutInflater.from(mContext);...

Android开发:使用DialogFragment实现dialog自定义布局

使用DialogFragment实现dialog的自定义布局最大的好处是可以更好控制dialog的生命周期。 TestFragment的代码: public class TestFragment extends DialogFragment { @Override public View onCreateView(LayoutInflate...

android——处理Google play因WebView SSL Error Handler alerts被拒的问题

    最近app上线,由于上述问题,被google市场给拒了。 看到这个,点进Google help Center article,应该知道问题所在了。直接在项目里找到使用webview的地方。找到webviewClient ,看看你有没有处理onReceivedSslError方法。怎么处理的。是不是按照规范。MD,要忙了。直接贴代码 public...

Electron学习笔记(十四)—— 常用api____dialog

https://www.electronjs.org/docs/api/dialog 对话框显示用于打开和保存文件、警报等的本机系统对话框。 在Electron的主线程上打开 const { dialog } = require('electron')console.log(dialog.showOpenDialog({ properties: ['ope...