温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android开发中ProgressDialog简单用法示例

发布时间:2020-09-23 16:42:59 来源:脚本之家 阅读:173 作者:guochongcan 栏目:移动开发

本文实例讲述了Android开发中ProgressDialog简单用法。分享给大家供大家参考,具体如下:

网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下:

建立android工程等工作都略去,Google一下就可以了。

下面来介绍主要的Activity

ProgressBarDemo.java

package com.lveyo.android.demo.progressbar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ProgressBarDemo extends Activity {
  private TextView statusTextView;
  private Button beginBtn;
  private ProgressDialog progressDialog;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    statusTextView = (TextView)findViewById(R.id.status);
    beginBtn = (Button)findViewById(R.id.beginBtn);
    setListener();
  }
  /**
   * 用Handler来更新UI
   */
  private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      //关闭ProgressDialog
      progressDialog.dismiss();
      //更新UI
      statusTextView.setText("Completed!");
    }};
  /**
   * 点击按钮事件listener
   */
  private void setListener(){
    beginBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //显示ProgressDialog
        progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);
        //新建线程
        new Thread(){
          @Override
          public void run() {
            //需要花时间计算的方法
            Calculation.calculate(4);
            //向handler发消息
            handler.sendEmptyMessage(0);
          }}.start();
      }
    });
  }
}

package com.lveyo.android.demo.progressbar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ProgressBarDemo extends Activity {
  private TextView statusTextView;
  private Button beginBtn;
  private ProgressDialog progressDialog;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    statusTextView = (TextView)findViewById(R.id.status);
    beginBtn = (Button)findViewById(R.id.beginBtn);
    setListener();
  }
  /**
   * 用Handler来更新UI
   */
  private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      //关闭ProgressDialog
      progressDialog.dismiss();
      //更新UI
      statusTextView.setText("Completed!");
    }};
  /**
   * 点击按钮事件listener
   */
  private void setListener(){
    beginBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //显示ProgressDialog
        progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);
        //新建线程
        new Thread(){
          @Override
          public void run() {
            //需要花时间计算的方法
            Calculation.calculate(4);
            //向handler发消息
            handler.sendEmptyMessage(0);
          }}.start();
      }
    });
  }
}

Calculation.java

package com.lveyo.android.demo.progressbar;
/**
 * 示意方法
 * @author lveyo
 *
 */
public class Calculation {
  public static void calculate(int sleepSeconds){
    try {
      Thread.sleep(sleepSeconds * 1000);
    } catch (Exception e) {
      // TODO: handle exception
    }
  }
}

package com.lveyo.android.demo.progressbar;
/**
 * 示意方法
 * @author lveyo
 *
 */
public class Calculation {
  public static void calculate(int sleepSeconds){
    try {
      Thread.sleep(sleepSeconds * 1000);
    } catch (Exception e) {
      // TODO: handle exception
    }
  }
}

main.xml文件

<?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:id="@+id/status"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<Button android:id="@+id/beginBtn"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="begin"
  />
</LinearLayout>

<?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:id="@+id/status"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<Button android:id="@+id/beginBtn"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="begin"
  />
</LinearLayout>

在android中,通常我们无法在单独的线程中更新UI,而要在主线程中,这也就是为什么我们要使用 Handler了,当handler收到消息中,它会把它放入到队列中等待执行,通常来说这会很快被执行。

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI