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()方法:
- @Override
- protected Dialog onCreateDialog(int id) {
- // ...
- }
- public void onClick(View v) {
- showDialog(0);
- }
想要创建一个对话框,还需要使用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
- 自定义Dialog android Dialog
- 【Android】Dialog以及自定义Dialog
- android 系统Dialog,自定义Dialog
- android 自定义dialog 自定义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
- Hibernate的元数据(不需要java类,动态建表,存储,读取)
- POJ 2272射靶谁赢
- Android 创建fragment时向fragment中传数据
- jquery的$.extend和$.fn.extend作用及区别
- 牧师丧女后分享“神不会浪费你的任何经历”
- android整合--自定义dialog
- Guice指南-架构概览
- cocos2d-x 输入框 简单实现
- BT5R3-GNOME-64中安装Flash插件……
- 人工智能 搜索算法
- js简单制作一个时钟
- ID/cpu/MI卡、非接触式读卡器资料集合
- 适宜于窗口访问模式的分布式瓦片数据存储系统TSS
- Hibernate Restrictions