Android学习笔记-Dialog详解 Full Screen DialogFragment (over ActionBar) in Android:传送门

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

扩展阅读: