我正在研究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>
答案 0 :(得分:2)
在onPreExecute()中将Visibility设置为Visible,在onPostExecute()中设置为Gone
@Override
protected void onPostExecute(String result) {
mProgress.setVisibility(View.GONE);
}
@Override
protected void onPreExecute() {
mProgress.setVisibility(View.VISIBLE);
}
编辑:在AsyncTask
中定义进度条。
ProgressBar mProgress;
为Asynctask创建构造函数并将progressbar引用传递给它。
public progressCheck(ProgressBar progressbar){
this.mProgress = progressbar;
}
在您的活动中称之为progressCheck obj = new progressCheck(mProgress);
EDIT2:MainActivity
public class MainActivity extends AppCompatActivity {
Button button1;
Public ProgressBar mProgress;
progressCheck obj;
@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);
obj = new progressCheck(mProgress);
mProgress.setVisibility(View.GONE);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
obj.execute();
}
});
}
答案 1 :(得分:1)
new AsyncTask<Void, Void, Void>
{
ProgressDialog pd = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.setTitle("Please wait");
pd.setMessage("Connecting to Google...");
pd.setCancelable(false);
pd.show();
}
@Override
protected Void doInBackground(Void... params) {
try{
Thread.sleep(YOUR_TIME);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
protected void onPostExecute(Void aVoid)
{
super.onPostExecute(aVoid);
if (pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
}.execute();
现在管理您的要求.. 希望这会有所帮助..
答案 2 :(得分:1)
从onClick()方法中删除mProgress
行。
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
obj.execute();
}
});
在onPreExecute()方法中添加这些mProgress可见性行,在onPostExecute()方法中添加mProgress Gone。
@Override
protected void onPostExecute(String result) {
mProgress.setVisibility(View.GONE);
}
@Override
protected void onPreExecute() {
mProgress.setVisibility(View.VISIBLE);
}
修改强>
将mProgress传递给progressCheck类。
progressCheck obj = new progressCheck(mProgress);
并在progressCheck构造函数中获取它。
ProgressBar progress;
public progressCheck(ProgressBar mProgress){
progress = mProgress;
}
Async ovrridden方法就像......
@Override
protected void onPostExecute(String result) {
progress.setVisibility(View.GONE);
}
@Override
protected void onPreExecute() {
progress.setVisibility(View.VISIBLE);
}
答案 3 :(得分:0)
你要做的就是在你的AsyncTask onPostExecute中编写这段代码。
ChatCell
完整代码
public class ChatCell extends ListCell<ChatItem> {
private ChatItemView view;
private InvalidationListener listViewListener;
private InvalidationListener listViewWidthListener;
public ChatCell() {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
listViewWidthListener = e -> {
updateItem(getItem(), isEmpty());
};
listViewListener = e -> {
if(getView() == null) return;
getView().unbindWidth();
if(listViewProperty().get() != null) {
getView().bindWidth(listViewProperty().get().widthProperty());
listViewProperty().get().widthProperty().addListener(listViewWidthListener);
listViewWidthListener.invalidated(listViewProperty().get().widthProperty());
}
};
}
public ChatItemView getView() {
return view;
}
private void initChatLabelView() {
view = new ChatLabelView();
}
private void initChatMessageView(ChatMessage message) {
view = new ChatMessageViewWrapper(message);
}
private void resetView() {
if(view == null) return;
view.unbindWidth();
listViewProperty().removeListener(listViewListener);
if(getListView() != null)
getListView().widthProperty().removeListener(listViewWidthListener);
view = null;
}
private void newViewCreated() {
listViewProperty().addListener(listViewListener);
setGraphic(view);
listViewListener.invalidated(listViewProperty());
}
@Override
protected void updateItem(ChatItem item, boolean empty) {
super.updateItem(item, empty);
if(item == null || empty) {
setGraphic(null);
setText(null);
resetView();
} else {
// First-level comparison: Label vs. Message
if(item.getViewType() == ViewType.LABEL) {
if(view == null || !(view instanceof ChatLabelView)) {
resetView();
initChatLabelView();
newViewCreated();
}
}
else if(item.getViewType() == ViewType.MESSAGE) {
ChatMessage message = (ChatMessage) item;
if(view == null || !(view instanceof ChatMessageViewWrapper)) {
resetView();
initChatMessageView(message);
newViewCreated();
}
else {
ChatMessageViewWrapper bubble = (ChatMessageViewWrapper) view;
// Second-level comparison: Self-message vs. other user message,
// Text vs. Image (or file), multiline vs. oneline
if( (bubble.getContentType() != message.getContentType())
|| (bubble.isSelf() ^ message.isSelf())
|| (bubble.isMultiline() ^ message.isMultiline())) {
resetView();
initChatMessageView(message);
newViewCreated();
}
}
}
else {
return;
}
view.setItem(item);
}
}
@Override
public Orientation getContentBias() {
return Orientation.HORIZONTAL;
}
}