android整合--自定义dialog
2013-09-14 15:50
204 查看
有的时候,可能需要弹出一个对话框,以便从用户的输入来获取某些确认信息。这种情况下,可以重写Activity基类中的受保护方法(protected)onCreateDialog()。
1、创建一个工程testone
2、main.xml 两个button 点击显示对话框
3、创建activity
点击第一个button显示进度条
点击第二个button显示自定义对话框
想要显示对话框,首先要重写Activity基类中的onCreateDialog()方法:
[java]
view plaincopy
@Override
protected Dialog onCreateDialog(int id) {
// ...
}
当调用showDialog()的时候,上面被重写的方法就被调用了:
[java]
view plaincopy
public void onClick(View v) {
showDialog(0);
}
这个创建对话框的onCreateDialog()方法是一个被Activity控制的回调函数,当调用showDialog()时,onCreateDialog()回调函数就被触发了。showDialog()方法接受一个Integer参数,用来识别到底要显示哪个对话框。一般情况下,使用switch语句去判断显示不同的对话框。
想要创建一个对话框,还需要使用AlertDialog类的Builder构造器,设置不同的属性,比如图标、标题、按钮、单选框等等:
源码下载地址:
http://download.csdn.net/download/nameyuxiang/6266571
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
@Override
protected Dialog onCreateDialog(int id) {
// ...
}
当调用showDialog()的时候,上面被重写的方法就被调用了:
[java]
view plaincopy
public void onClick(View v) {
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
相关文章推荐
- Android之加载视图(二)自定义Dialog
- android 自定义Dialog
- android 自定义Dialog去除黑边
- Android 简单的自定义Dialog
- Android 自定义dialog样式位置大小,判断是否第一次登陆
- Android中自定义Dialog样式
- Android自定义dialog可选择展示年月日时间选择栏
- android初学-----自定义Dialog弹出框(没有黑色背景)
- Android dialog自定义样式
- Android自定义对话框(Dialog)
- Android 自定义符合软件整体风格的dialog
- Android--自定义弹出框-自定义dialog
- android dialog 两种自定义布局风格
- Android自定义对话框(Dialog)位置,大小
- Android中利用Activity自定义背景透明、大小和位置Dialog
- android 通过回调实现 完全自定义Dialog 简单易用
- Android编程中自定义dialog用法实例
- Android自定义Dialog,Toast,Notification和PopupWindow
- Android自定义对话框(Dialog)位置,大小
- Android之自定义dialog