zoukankan      html  css  js  c++  java
  • Android学习笔记(九)——更复杂的进度对话框

    显示操作进度的对话框


    1、使用上一篇创建的同一项目。在activity_main.xml文件里加入一个Button:

        <Button
            android:id="@+id/btn_dialog3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick3"
            android:text="Click to display a detailed progress dialog" />

    2、在MainActivity.java文件里加入详细的进度条实现代码:

    首先加入onClick3()方法:

    public void onClick3(View v) {
    		showDialog(1);// id为1,在调用回调方法onCreateDialog()时。将id传进去。使其选择case 1情况。
    		progressDialog.setProgress(0);// 从0開始
    
    		new Thread(new Runnable() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				for (int i = 1; i <= 10; i++) {
    					try {
    						Thread.sleep(1000);
    						progressDialog.incrementProgressBy(100 / 10);// 步进为10
    					} catch (InterruptedException e) {
    						// TODO: handle exception
    						e.printStackTrace();
    					}
    				}
    				progressDialog.dismiss();// 销毁对话框
    			}
    		}).start();
    	}
    再在onCreateDialog()回调方法中加入id=1时的代码(即实现显示操作进度对话框的代码):

    case 1:
    			progressDialog = new ProgressDialog(this);
    			progressDialog.setIcon(R.drawable.ic_launcher);
    			progressDialog.setTitle("Downloading files...");
    			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条样式
    			progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",// 设置OKbutton
    					new DialogInterface.OnClickListener() {
    
    						@Override
    						public void onClick(DialogInterface dialog, int which) {
    							// TODO Auto-generated method stub
    							Toast.makeText(getBaseContext(), "OK clicked!",
    									Toast.LENGTH_SHORT).show();
    						}
    					});
    			progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",// 设置取消button
    					new DialogInterface.OnClickListener() {
    
    						@Override
    						public void onClick(DialogInterface dialog, int which) {
    							// TODO Auto-generated method stub
    							Toast.makeText(getBaseContext(), "Cancel clicked!",
    									Toast.LENGTH_SHORT).show();
    						}
    					});
    			return progressDialog;

    3、执行一下,效果例如以下:



    点击此处下载完整代码~

  • 相关阅读:
    循环的注意点
    c语言实践输出某个区间中不是3的倍数的偶数
    while循环for循环优缺点和应用
    while 和do while循环的区别
    多重if else和switch case的区别
    if else的执行流程
    多个if和一个ifelse的区别
    对两个变量排序,从小到大输出
    【译】第四篇 Integration Services:增量加载-Updating Rows
    【译】第三篇 Integration Services:增量加载-Adding Rows
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5150394.html
Copyright © 2011-2022 走看看