首页 > 技术文章 > android进度条的使用

qgc88 2013-09-22 12:52 原文

// 导入按钮事件
  btnsearch.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    AlertDialog.Builder builder = new Builder(ContentActivity.this);
    builder.setTitle("请选择导入类型");
    builder.setNegativeButton("导入所有号码", new OnClickListener() {
     public void onClick(DialogInterface dialog, int which) {
      inpPhoneShow("phone");// 导入所有号码的方法
     }
    });
    builder.setNeutralButton("导入SIM卡号码", new OnClickListener() {
     public void onClick(DialogInterface dialog, int which) {
      inpPhoneShow("sim");// 导入所有号码的方法
     }
    });
    builder.show();
   }
   
  });

 

// 导入号码前弹框
  public void inpPhoneShow(final String inpChice) {
   List<Personer> personer = new ArrayList<Personer>();
   if ("phone".equals(inpChice)) {
    personer = getPhoneContant();// 查询所有手机号码
   } else if ("sim".equals(inpChice)) {
    personer = getSimContant();// 查询SIM号码
   }
   final Activity activity = new Activity();
   AlertDialog.Builder builder = new Builder(ContentActivity.this);
   builder.setPositiveButton("确定", new OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {

     new AsyncAdd(ContentActivity.this).execute(inpChice);
    }
   });
   builder.setNegativeButton("取消", new OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
     activity.finish();// 销毁当前
    }
   });
   builder.setTitle("将导入" + personer.size() + "个号码");
   builder.show();
  }

 

 

 //进度条
    class AsyncAdd extends AsyncTask<String, Integer, Void>{
     
     private ProgressDialog dialog;
     public AsyncAdd(Context ctx){
      dialog = new ProgressDialog(ctx);
      //设置进度条风格,风格为圆形,旋转的
      dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      //设置ProgressDialog 标题
      dialog.setTitle("导入联系人");
      //设置ProgressDialog 提示信息
      dialog.setMessage("正在导入联系人请稍候....");
      //设置ProgressDialog 标题图标
      dialog.setIcon(android.R.drawable.ic_dialog_alert);
      //设置ProgressDialog的最大进度
      dialog.show();
     }
     
     @Override
     protected Void doInBackground(String... params) {
      // TODO Auto-generated method stub
      String inpChice = params[0];
//      return null;
      List<Personer> personer = new ArrayList<Personer>();
      if ("phone".equals(inpChice)) {
       personer = getPhoneContant();// 查询所有手机号码
      } else if ("sim".equals(inpChice)) {
       personer = getSimContant();// 查询SIM号码
      }
      int i =0;
      for(Personer p : personer){
       saveInfo(p.getPersonName(), p.getPhone());
       publishProgress(personer.size(),i);
       i++;
      }
      return null;
     }
     
     @Override
     protected void onProgressUpdate(Integer... values) {
      // TODO Auto-generated method stub
      super.onProgressUpdate(values);
      dialog.setMax(values[0]);
      dialog.setProgress(values[1]);
     }
     
     @Override
     protected void onPostExecute(Void result) {
      // TODO Auto-generated method stub
      super.onPostExecute(result);
      if(dialog != null){
       dialog.dismiss();
      }
     }
     
    }

推荐阅读