progressDialog 为什么设置了setProgress()方法无反应?
============问题描述============
package com.example.dialoglogdemo; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends ActionBarActivity implements OnClickListener { private AlertDialog.Builder builder; ProgressDialog pd; private Button btn_start; private Button btn_stop; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case 1: if (pd != null) { int value = msg.arg1; Log.i("updateValue", "取出的值:"+msg.arg1+""); pd.setProgress(msg.arg1);//无反应,不更新进度 } break; case 2: if (builder != null) { } break; case 3: if (pd != null) { pd.dismiss(); } default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_start = (Button) findViewById(R.id.button1); btn_stop = (Button) findViewById(R.id.button2); btn_start.setOnClickListener(this); btn_stop.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: // 开始显示 pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setTitle("下载"); pd.setIcon(R.drawable.ic_launcher); pd.setMessage("正在下载"); pd.setIndeterminate(true); pd.setCancelable(true); pd.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub // pd.dismiss(); } }); pd.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub // pd.dismiss(); } }); pd.show(); // pd = ProgressDialog.show(MainActivity.this, "Loading...", // "Please wait...", true, false); new Thread(new Runnable() { @Override public void run() { Message msg = new Message(); // TODO Auto-generated method stub try { for (int i = 1; i <= 100; i++) { Thread.sleep(100); msg=Message.obtain(); msg.what=1; msg.arg1 = i; Log.i("updateValue", "放入的值:"+msg.arg1+""); mHandler.sendMessage(msg); } mHandler.sendEmptyMessage(3); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); break; case R.id.button2: // 停止显示 builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("下载") .setIcon(R.drawable.ic_launcher) .setCancelable(true) .setMessage("downloading...") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setNegativeButton("取消下载", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); AlertDialog dialog = builder.create(); dialog.show(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(8000); mHandler.sendMessage(mHandler.obtainMessage(2)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); break; default: break; } } }
============解决方案1============
初始化时加上pd.setMax(最大的值);
============解决方案2============
没看到你 的 setMax
posted on 2014-11-08 12:53 android基础教程 阅读(1213) 评论(0) 编辑 收藏 举报