android进度对话框的使用
16lz
2021-12-04
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DialogActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text" /> <Button android:id="@+id/btn_dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="多选对话框" /> <Button android:id="@+id/btn_dialog1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick1" android:text="进度对话框" /> <Button android:id="@+id/btn_dialog2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick2" android:text="详细进度对话框" /> </LinearLayout>
DialogActivity.java
package com.dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class DialogActivity extends Activity { //CharSequence接口 CharSequence[] items = {"谷歌","苹果","百度","微软"}; boolean[] itemsChecked = new boolean[items.length]; //创建ProgressDialog类的一个实例 ProgressDialog progressDialog = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } //重写onCreateDialog()方法 protected Dialog onCreateDialog(int id, Bundle args) { switch (id) { case 0: /* * AlertDialog Dialog的子类 显示进度指示器和可选的文本消息或视图的对话框 * 多选对话框 */ Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("你喜欢哪家互联网公司"); //响应确定按钮 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //Toast并没有在DialogActivity直接使用,而是在AlertDialog类中使用 因此需要用getBaseContext()返回一个Context类的实例 public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "确定按钮被点击", Toast.LENGTH_SHORT).show(); } }); //响应取消按钮 builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "取消按钮被点击", Toast.LENGTH_SHORT).show(); } }); //响应多选事件 builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getBaseContext(), (isChecked?"选中":"未选中")+items[which], Toast.LENGTH_SHORT).show(); } }); return builder.create(); case 1: /* * 设置图标 标题 样式 按钮 * 显示详细进度对话框 */ progressDialog = new ProgressDialog(this); progressDialog.setIcon(R.drawable.ic_launcher); progressDialog.setTitle("下载文件..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show(); } }); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show(); } }); return progressDialog; } return null; } public void onClick(View v){ //调用onCreateDialog()方法 showDialog(0); } /* * 进度对话框 */ public void onClick1(View v){ final ProgressDialog dialog = ProgressDialog.show(this, "请等待3秒", "Please wait...",true); //启动一个新线程 new Thread(new Runnable() { public void run() { try { Thread.sleep(3000); //解除对话框 从屏幕上移除 此方法是线程安全的 dialog.dismiss(); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } /* * 详细进度对话框 */ public void onClick2(View v){ showDialog(1); //设置初始值为0 progressDialog.setProgress(0); new Thread(new Runnable() { public void run(){ for (int i = 1; i <= 15; i++) { try { Thread.sleep(1000); //更新对话框 progressDialog.incrementProgressBy((int)100/15); } catch (InterruptedException e) { e.printStackTrace(); } } progressDialog.dismiss(); } }).start(); } }
随笔系原创 转载请注明
欢迎访问我的博客
更多相关文章
- Android(安卓)Studio点击按钮更换背景图片
- Android(安卓)进度条
- 布局技巧和列表控件
- android进度条的样式
- android代码实现背景切换
- Android(安卓)Studio创建计算器Demo
- 2.5.3 使用alertDialog创建自定义对话框
- android 设置Button或者ImageButton的背景透明
- Android(安卓)API 中文(13) —— ToggleButton