我正在开发一个Android应用程序,我需要在循环执行完成之前显示ProgressBar。执行后,它将转到nextActivity。
这是我的java代码
public class Image_Recognition extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image__recognition);
Button btnNextPage = (Button) findViewById(R.id.btnNextPage);
btnNextPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> items = textRecognizer.detect(frame);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < items.size(); ++i) {
TextBlock item = items.valueAt(i);
stringBuilder.append(item.getValue());
stringBuilder.append("\n");
}
stringBuilder.append('.');
txtResult.setText(stringBuilder.toString());
str = stringBuilder.toString();
Intent myintent = new Intent(Image_Recognition.this, ReminderAddActivity.class);
myintent.putExtra("translate", str);
startActivity(myintent);
}
});
}
//}
}
答案 0 :(得分:0)
这里有两个问题。
ProgressBar ,您可以在XML中使用ProgressDialog或ProgressBar并显示它,同时隐藏其余的IU。
阻止用户界面
您正在直接从您点击监听器运行您的循环,这意味着您将阻止您的UI,即使您显示某种进展,它将被阻止,直到您的循环结束,您需要启动一个线程并运行你的循环。
这是你点击监听器impl
的一个不太干净的版本final ProgressDialog dialog = new ProgressDialog(Image_Recognition.this);
dialog.show();
new Thread(new Runnable() {
@Override
public void run() {
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> items = textRecognizer.detect(frame);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < items.size(); ++i) {
TextBlock item = items.valueAt(i);
stringBuilder.append(item.getValue());
stringBuilder.append("\n");
}
stringBuilder.append('.');
txtResult.setText(stringBuilder.toString());
str = stringBuilder.toString();
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
Intent myintent = new Intent(Image_Recognition.this, ReminderAddActivity.class);
myintent.putExtra("translate", str);
startActivity(myintent);
}
});
}
}).start();
答案 1 :(得分:0)
您可以使用Async Task并在doInBackground()
中运行循环class MyTask extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
public MyTask(ProgressDialog pd){
this.pd=pd;
}
@Override
protected void onPreExecute () {
super.onPreExecute();
pd.setMessage("Uploading . . .");
pd.show();
pd.setCancelable(false);
}
@Override
protected Void doInBackground (Void...voids){
for(int i=0;i<10000000;i++){
System.out.print(i);
}
return null;//download file
}
@Override
protected void onPostExecute (Void aVoid){
super.onPostExecute(aVoid);
pd.dismiss();
}
}
并在活动中致电new MyTask(pd).execute();
答案 2 :(得分:0)
你可以试试这个。
final ProgressDialog dialog = new ProgressDialog(Image_Recognition.this);
btnNextPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.show(Image_Recognition.this, "", "Please wait...", true);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
TextRecognizer textRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if (!textRecognizer.isOperational())
Log.e("ERROR", "Detector dependencies are not yet available");
else {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> items = textRecognizer.detect(frame);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < items.size(); ++i) {
TextBlock item = items.valueAt(i);
stringBuilder.append(item.getValue());
stringBuilder.append("\n");
}
stringBuilder.append('.');
txtResult.setText(stringBuilder.toString());
str = stringBuilder.toString();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
dialog.setCancelable(true);
Intent myintent = new Intent(Image_Recognition.this, ReminderAddActivity.class);
myintent.putExtra("translate", str);
startActivity(myintent);
}
});
}
},5000);