未显示进度对话框
我有这段代码片段,这段代码的问题是当我单击“完成”按钮时 进度对话框未显示。请帮忙
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
done = (Button)findViewById(R.id.sendbut);
done.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showProgressDialog();
query_str = getString();
startUploading();
}
});
}
public void showProgressDialog(){
GlobalClass.printLine("Showing progress dialog");
progressDialog = new ProgressDialog(Send.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Sending...");
progressDialog.setCancelable(false);
progressDialog.show();
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
progressDialog.setProgress(mProgressStatus);
Toast.makeText(getApplicationContext(), "Herererererer",Toast.LENGTH_LONG).show();
if(mProgressStatus == 100){
Toast.makeText(getApplicationContext(), "Done",Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
}
}).start();
}
I have this code snippet, the problem with this code is that when I click the 'done' button
the ProgressDialog is not showing.Please help
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
done = (Button)findViewById(R.id.sendbut);
done.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showProgressDialog();
query_str = getString();
startUploading();
}
});
}
public void showProgressDialog(){
GlobalClass.printLine("Showing progress dialog");
progressDialog = new ProgressDialog(Send.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Sending...");
progressDialog.setCancelable(false);
progressDialog.show();
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
progressDialog.setProgress(mProgressStatus);
Toast.makeText(getApplicationContext(), "Herererererer",Toast.LENGTH_LONG).show();
if(mProgressStatus == 100){
Toast.makeText(getApplicationContext(), "Done",Toast.LENGTH_LONG).show();
progressDialog.dismiss();
}
}
});
}
}).start();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过使用
AsyncTask
?这是文档: http://developer.android.com/reference/android/ os/AsyncTask.html
您可能想用 AsyncTask 替换线程。
Did you try to use
AsyncTask
?Here is the doc: http://developer.android.com/reference/android/os/AsyncTask.html
You may want to replace the thread with the AsyncTask.
您应该使用 AsyncTask,它也称为无痛线程。
现在,要在后台处理时显示进度条,下面是一个示例:
You should use AsyncTask which is also known as Painless threading.
Now, to show progressbar while processing in background, here is an example:
@Hari ..
我认为您正在从startUploading()内部调用另一个函数,这就是ProgressDialog不显示的原因。尝试在 XML 中创建进度条并将所有函数写入按钮单击内。如果这是一个递归函数,请更改您的逻辑以在按钮单击本身内调用它。
@Hari..
I think you are calling another functions from inside startUploading(),that's why ProgressDialog is not shown. Try creating the progressBar in XML and write all functions inside the button click. If that is a recursive function change your logic to call this inside the button click itself.
Paresh Mayani 上面发布的答案将不起作用,因为您无法
在 AsyncTask 中使用。 “this”需要是一个上下文。
The answer posted above by Paresh Mayani will not work since you can't use
in the AsyncTask. "this" needs to be a Context.