我正在尝试显示ProgressBar。
我是Android初学者。
当我按下按钮时,任务应该在后台运行,但它不会显示ProgressBar。
有什么问题?我无法理解。
请帮助我!
package com.example.shikkok_services;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btnThread,btntask;
private ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnThread=(Button)findViewById(R.id.btnStartThread);
btntask=(Button)findViewById(R.id.btntsk);
btntask.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//start myTast
new MyTask(MainActivity.this).execute();
}
});
}
}
package com.example.shikkok_services;
import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MyTask extends AsyncTask<Void, Integer, Void> {
Context context;
Handler handler;
Dialog dialog;
TextView txtprogrss;
ProgressBar progress;
Button btnCancel;
MyTask(Context context, Handler handler){
this.context=context;
this.handler=handler;
}
MyTask(Context context){
this.context=context;
this.handler=handler;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// create dialog
dialog=new Dialog(context);
dialog.setCancelable(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pogressdialog);
txtprogrss=(TextView) dialog.findViewById(R.id.txtProgress);
progress=(ProgressBar)dialog.findViewById(R.id.progressBar2);
btnCancel=(Button)dialog.findViewById(R.id.btnProgress);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyTask.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(Void... arg0) {
for (int i = 0; i < 100; i++) {
if(isCancelled()){
break;
}else{
Log.e("In Background","current value;"+ i);
publishProgress(i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progress.setProgress(values[0]);
txtprogrss.setText("progress update"+ values[0]+"%");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:4)
您没有使用dialog.show()
的{{1}}方法拨打onPreExecute
。
答案 1 :(得分:4)
您忘记在dialog.show()
onPreExecute()
了
答案 2 :(得分:2)
使用ProgressDialog
。在这种情况下,您不需要任何布局。
ProgressDialog progressDialog = new ProgressDialog(context);
在onPreExecute
中显示
progressDialog.show();
并在onPostExecute
拒绝
progressDialog.dismiss();
答案 3 :(得分:1)
添加到OnPostExecute方法:
pg.setVisibility(View.INVISIBLE);
添加到onPreExecute方法:
pg.setVisibility(View.VISIBLE);
并在布局文件中添加到进度条:
android:visibility="invisible"
答案 4 :(得分:1)
您创建了对话框但未在任何位置显示。你的onPreExecute()应如下所示:
@Override
protected void onPreExecute() {
super.onPreExecute();
// create dialog
dialog=new Dialog(context);
dialog.setCancelable(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.pogressdialog);
txtprogrss=(TextView) dialog.findViewById(R.id.txtProgress);
progress=(ProgressBar)dialog.findViewById(R.id.progressBar2);
btnCancel=(Button)dialog.findViewById(R.id.btnProgress);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyTask.this.cancel(true);
dialog.dismiss(); //On button click cancel AsyncTask and dismiss dialog
}
});
dialog.show(); //Show the dialog
}
单击btnCancel时,您还需要关闭对话框。