【发布时间】:2016-12-26 04:25:43
【问题描述】:
我正在处理Android。我想在与www.google.com 的连接成功后仅显示进度条 10 秒。这是我的代码。任何帮助表示赞赏。
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
Button button1;
Public ProgressBar mProgress;
progressCheck obj = new progressCheck(mProgress);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button fab = (Button) findViewById(R.id.button1);
mProgress =(ProgressBar) findViewById(R.id.progressBar);
mProgress.setVisibility(View.GONE);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
obj.execute();
}
});
}
ProgressCheck.java 扩展 AsyncTask
import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class progressCheck extends AsyncTask<String, String, String> {
ProgressBar mProgressBar;
public progressCheck(ProgressBar p){
this.mProgressBar = p;
}
@Override
protected String doInBackground(String... params) {
Document doc = null;
String url = "https://www.google.com.pk/";
try {
doc = Jsoup.connect(url).timeout(20 * 1000).get();
Thread.sleep(10000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
mProgressBar.setVisibility(View.GONE);
}
@Override
protected void onPreExecute() {
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(String... text) {
}
}
Content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.namal.circularprogressbar2.MainActivity"
tools:showIn="@layout/activity_main">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:indeterminate="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/button1"
android:layout_below="@+id/progressBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
</RelativeLayout>
【问题讨论】:
-
您要么需要来自
AsyncTask的回调,要么给它一个对您的ProgressBar的引用,以便在完成后禁用它。
标签: android visibility android-progressbar