android整合--自定义dialog

来源:互联网 发布:手写识别 算法 编辑:程序博客网 时间:2024/05/29 19:15

有的时候,可能需要弹出一个对话框,以便从用户的输入来获取某些确认信息。这种情况下,可以重写Activity基类中的受保护方法(protected)onCreateDialog()。

1、创建一个工程testone

2、main.xml 两个button 点击显示对话框

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    />    <Button    android:id="@+id/btnprogress"    android:layout_width="fill_parent"        android:layout_height="wrap_content"          android:onClick="onclick"       android:text="点击显示进度条"        />            <Button    android:id="@+id/btndialog"    android:layout_width="fill_parent"        android:layout_height="wrap_content"          android:onClick="onclick1"       android:text="点击显示自定义对话框"        /></LinearLayout>

3、创建activity


    /** Called when the activity is first created. */    ProgressDialog progress = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.main);    }        public void onclick(View view){        showDialog(0);        progress.setProgress(0);        new Thread(                new Runnable(){                    public void run(){                        for(int i = 1;i <= 15;i++){                            try {                                Thread.sleep(1000);                                progress.incrementProgressBy((int) 100/15);                            } catch (InterruptedException e) {                                // TODO Auto-generated catch block                                e.printStackTrace();                            }                                                    }                                                progress.dismiss();                                        }                }                ).start();                }    public void onclick1(View view){        showDialog(1);    }    /* (non-Javadoc)     * @see android.app.Activity#onCreateDialog(int)     */    @Override    protected Dialog onCreateDialog(int id) {        // TODO Auto-generated method stub    switch(id){    case 0:        progress = new ProgressDialog(this);        progress.setTitle("progress bar");        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);        progress.setButton(DialogInterface.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {                        @Override            public void onClick(DialogInterface dialog, int which) {                // TODO Auto-generated method stub                Toast.makeText(getBaseContext(), "you clicked the ok button", Toast.LENGTH_LONG).show();            }        });        progress.setButton(DialogInterface.BUTTON_NEGATIVE, "cancel", new DialogInterface.OnClickListener() {                        @Override            public void onClick(DialogInterface dialog, int which) {                // TODO Auto-generated method stub                Toast.makeText(getBaseContext(), "you clicked the cancel button", Toast.LENGTH_LONG).show();            }        });          progress.setButton(DialogInterface.BUTTON_NEUTRAL, "neutral", new DialogInterface.OnClickListener() {                        @Override            public void onClick(DialogInterface dialog, int which) {                // TODO Auto-generated method stub                Toast.makeText(getBaseContext(), "you clicked the neutral button", Toast.LENGTH_LONG).show();            }        });        return progress;        case 1:        return new AlertDialog.Builder(this)        .setTitle("the dialog")        .setIcon(R.drawable.icon)        .setPositiveButton("ok", new DialogInterface.OnClickListener() {                        @Override            public void onClick(DialogInterface dialog, int which) {                // TODO Auto-generated method stub                Toast.makeText(getBaseContext(), "you clicked the dialog ok button", Toast.LENGTH_LONG).show();            }        })        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {                        @Override            public void onClick(DialogInterface dialog, int which) {                // TODO Auto-generated method stub                Toast.makeText(getBaseContext(), "you clicked the dialog cancel button", Toast.LENGTH_LONG).show();            }        }).create();    }    return null;    }       

点击第一个button显示进度条

点击第二个button显示自定义对话框

想要显示对话框,首先要重写Activity基类中的onCreateDialog()方法:

[java] view plaincopy
  1. @Override  
  2. protected Dialog onCreateDialog(int id) {  
  3.     // ...  
  4. }  
当调用showDialog()的时候,上面被重写的方法就被调用了:
[java] view plaincopy
  1. public void onClick(View v) {  
  2.     showDialog(0);  

这个创建对话框的onCreateDialog()方法是一个被Activity控制的回调函数,当调用showDialog()时,onCreateDialog()回调函数就被触发了。showDialog()方法接受一个Integer参数,用来识别到底要显示哪个对话框。一般情况下,使用switch语句去判断显示不同的对话框。

想要创建一个对话框,还需要使用AlertDialog类的Builder构造器,设置不同的属性,比如图标、标题、按钮、单选框等等:

    @Override      protected Dialog onCreateDialog(int id) {          switch (id) {          case 0:              Builder builder = new AlertDialog.Builder(this);              builder.setIcon(R.drawable.ic_launcher);              builder.setTitle("This is a dialog with some simple text...");              builder.setPositiveButton("OK",                      new DialogInterface.OnClickListener() {                          public void onClick(DialogInterface dialog,                                  int whichButton) {                              Toast.makeText(getBaseContext(), "OK clicked!",                                      Toast.LENGTH_SHORT).show();                          }                      });                builder.setNegativeButton("Cancel",                      new DialogInterface.OnClickListener() {                          public void onClick(DialogInterface dialog,                                  int whichButton) {                              Toast.makeText(getBaseContext(), "Cancel clicked!",                                      Toast.LENGTH_SHORT).show();                          }                      });                builder.setMultiChoiceItems(items, itemsChecked,                      new DialogInterface.OnMultiChoiceClickListener() {                          public void onClick(DialogInterface dialog, int which,                                  boolean isChecked) {                              Toast.makeText(                                      getBaseContext(),                                      items[which]                                              + (isChecked ? " checked!"                                                      : " unchecked!"),                                      Toast.LENGTH_SHORT).show();                          }                      });              return builder.create();          }          return null;      }  

源码下载地址:

http://download.csdn.net/download/nameyuxiang/6266571

原创粉丝点击