显示进度条加载 [英] Display progress bar while loading

查看:124
本文介绍了显示进度条加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中将链接到另一个XML其中包括从服务器上的信息main.xml中的一个按钮。我有进度条来避免黑屏的同时,系统加载的信息。我已经做了code如下,但它仍然不是我想要的东西。在code以下将等待1000毫秒,然后才会执行下一个code。我怎么可以修改它,使加载等待时间的意愿取决于网速,如果互联网连接速度很慢,那么进度条,屏幕上会显示更长的时间。

 包com.android.myApps;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.os.Handler;
进口android.view.View;
进口android.widget.TextView;

公共类MainScr延伸活动{

    私人最终诠释WAIT_TIME = 1000;

    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.MainScr);
    }

    公共无效onClickCategory(查看视图)
    {
        findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);
        新的处理程序()。postDelayed(新的Runnable(){
            @覆盖
                公共无效的run(){
                      意图mainIntent =新的意图(MainScr.this,Category.class);
                      MainScr.this.startActivity(mainIntent);
                      MainScr.this.finish();
                      }
            },WAIT_TIME);
    }
}
 

解决方案

你在这里做的错误是你正在倾倒的具体时间到code 你永远不知道它会搭多大得到响应。 您应该按照如下方法

在屏幕上步骤1显示进度对话框

步骤2令下载走好自己的time.But应该在新的线程来完成

步骤3下载完成后,将提高消息的任务完成了,现在删除        进度对话框并继续。

我粘贴示例code here.Hope它会帮助你。

 包com.android.myApps;

进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.os.Handler;
进口android.os.Message;

公共类MainScr扩展活动
{
    私人处理程序处理程序;
    私人ProgressDialog进展情况;
    私人上下文的背景下;

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        上下文= AncActivity.this;
        进度=新ProgressDialog(本);
        progress.setTitle(请等一下!);
        progress.setMessage(等一下!);
        progress.setCancelable(假);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        处理程序=新的处理程序()
        {

            @覆盖
            公共无效的handleMessage(信息MSG)
            {
                progress.dismiss();
                意图mainIntent =新的意图(背景下,Category.class);
                startActivity(mainIntent);
                super.handleMessage(MSG);
            }

        };
        progress.show();
        新的Thread()
        {
            公共无效的run()
            {
                //在这里写下你的下载逻辑
                //在最后写这个。
                handler.sendEmptyMessage(0);
            }

        }。开始();

    }

}
 

I have one button in the main.xml which will link to another xml which include information from server. I include progress bar to avoid the blank screen while the system is loading the information. i already done the code as below but it's still not the things i wanted. the code below will "WAIT" for 1000 ms then only will execute the next code. how can i modify it so that the loading "WAIT TIME" will depends on the internet speed, if internet connection is slow, then the progress-bar-screen will show longer.

package com.android.myApps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class MainScr extends Activity {

    private final int WAIT_TIME = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {       
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.MainScr);       
    }   

    public void onClickCategory(View view)
    {
        findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable(){
            @Override
                public void run() {                          
                      Intent mainIntent = new Intent(MainScr.this, Category.class); 
                      MainScr.this.startActivity(mainIntent); 
                      MainScr.this.finish(); 
                      } 
            }, WAIT_TIME);
    }
}

解决方案

The mistake you are doing here is you are dumping specific time into your code You never know how much it will take to get response. You should follow following approach

Step 1 Show progress dialog on screen

Step 2 Let download take its own time.But it should be done in new thread

Step 3 Once download is complete it will raise message that task is done,now remove that progress dialog and proceed.

I am pasting sample code here.Hope it will help you.

package com.android.myApps;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class MainScr extends Activity
{
    private Handler handler;
    private ProgressDialog progress;
    private Context context;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = AncActivity.this;
        progress = new ProgressDialog(this);
        progress.setTitle("Please Wait!!");
        progress.setMessage("Wait!!");
        progress.setCancelable(false);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        handler = new Handler()
        {

            @Override
            public void handleMessage(Message msg)
            {
                progress.dismiss();
                Intent mainIntent = new Intent(context, Category.class);
                startActivity(mainIntent);
                super.handleMessage(msg);
            }

        };
        progress.show();
        new Thread()
        {
            public void run()
            {
                // Write Your Downloading logic here
                // at the end write this.
                handler.sendEmptyMessage(0);
            }

        }.start();

    }

}

这篇关于显示进度条加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆