我正在尝试在我的函数中加载进度条。 我有一个在加载时连接外部数据库的功能,我需要一个进度条。
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progress_bar"
android:layout_gravity="center"
android:max="100"
android:progress="50" />
<Button
android:id="@+id/botonAcceder"
android:layout_width="@dimen/width_boto_acceptar_intro_cod"
android:layout_height="@dimen/height_input_intro_cod"
android:background="@drawable/fondosubmit"
android:text="@string/aceptar"
android:onClick="irFormularioReserva"
android:textSize="30dp"
android:textColor="#000000"
android:layout_gravity="center_horizontal|bottom"
android:layout_below="@+id/frameLayout5"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/margin_top_boto_acceptar_intro_cod"/>
public void irFormularioReserva(View v){ /* mProgress = (ProgressBar) findViewById(R.id.progress_bar); // Start lengthy operation in a background thread new Thread(new Runnable() { public void run() { while (mProgressStatus < 100) { mProgressStatus = thread(); // Update the progress bar mHandler.post(new Runnable() { public void run() { mProgress.setProgress(mProgressStatus); } }); } } }).start();*/ //FUNCIONA NO BORRAR final httpHandler handler = new httpHandler(); Thread thread = new Thread() { @Override public void run() { final String txt = handler.post("http://www.url.com/page.php", "codigo", codigoInput.getText().toString()); final TextView t = (TextView) findViewById(R.id.textView); runOnUiThread(new Runnable() { @Override public void run() { //t.setText(txt); /* #### REDIRECCIÓN A OTRA ACTIVIDAD ##### */ Intent act = new Intent(IntroduceCodigo.this, formularioReserva.class); act.putExtra("codigo", txt); startActivity(act); finish(); /* #### FIN REDIRECCIÓN A OTRA ACTIVIDAD ##### */ // t.setText(txt); //System.out.println(txt); } }); } }; thread.start(); }
答案 0 :(得分:0)
正如323go所说。您可以使用AsyncTask方法,它将为您提供网络内容的前后阶段。很容易,您可以显示和解除progressdialog。
class ServerAsync extends AsyncTask<String, Integer, String>{
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
//stat the progress dialog
mProgressDialog = ProgressDialog.show(context, title, message);
}
@Override
protected String doInBackground(String... params) {
// Do your server stuff
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//run your progressbar through some time/counter
}
@Override
protected void onPostExecute(String result) {
//dismiss after finished all server stuffs
mProgressDialog.dismiss();
}
}