进度条问题

发布于 2024-10-15 10:09:31 字数 1286 浏览 3 评论 0原文

我有一个按钮,onclick 可以读取并向用户显示包含 rss 新闻的列表。我想添加一个进度条直到完成加载。我的问题是,如果我添加一个栏,它一开始会正常工作,但如果我按后退按钮,栏会再次启动并且无法停止。任何演示进度条的帮助或教程将不胜感激。谢谢!

ProgressBar myProgressBar;
  int myProgress = 0;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main1);

  Button nea = (Button) findViewById(R.id.nea);
   nea.setOnClickListener(new View.OnClickListener() {
   public void onClick (View view) {
    setContentView(R.layout.bar);

     myProgressBar=(ProgressBar)findViewById(R.id.bar);

           new Thread(myThread).start();

    Intent myIntent = new Intent(view.getContext(), nea.class);
    startActivityForResult(myIntent, 0);
    }  
  });
}

private Runnable myThread = new Runnable(){    
    @Override
    public void run() {
     // TODO Auto-generated method stub
     while (myProgress<100){
      try{
       myHandle.sendMessage(myHandle.obtainMessage());
       Thread.sleep(1000);
      }
      catch(Throwable t){
      }
     }
    }

    Handler myHandle = new Handler(){

     public void handleMessage(Message msg) {
      // TODO Auto-generated method stub
      myProgress++;
      myProgressBar.setProgress(myProgress);
     }
    };
      };

I have a button that onclick reads and shows to the user a list with rss news. I would like to add a progress bar until finish loading. My problem is that, if I add a bar, it works properly at first but if I press the back button, the bar starts again and it can't stop. Any help or a tutorial that demonstrates a progressbar would be appreciated. Thanks!

ProgressBar myProgressBar;
  int myProgress = 0;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main1);

  Button nea = (Button) findViewById(R.id.nea);
   nea.setOnClickListener(new View.OnClickListener() {
   public void onClick (View view) {
    setContentView(R.layout.bar);

     myProgressBar=(ProgressBar)findViewById(R.id.bar);

           new Thread(myThread).start();

    Intent myIntent = new Intent(view.getContext(), nea.class);
    startActivityForResult(myIntent, 0);
    }  
  });
}

private Runnable myThread = new Runnable(){    
    @Override
    public void run() {
     // TODO Auto-generated method stub
     while (myProgress<100){
      try{
       myHandle.sendMessage(myHandle.obtainMessage());
       Thread.sleep(1000);
      }
      catch(Throwable t){
      }
     }
    }

    Handler myHandle = new Handler(){

     public void handleMessage(Message msg) {
      // TODO Auto-generated method stub
      myProgress++;
      myProgressBar.setProgress(myProgress);
     }
    };
      };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柏林苍穹下 2024-10-22 10:09:31

实际上,我会使用 AsyncTask 来实现这一点,您可以在 onPreExecute() 中启动进度条,在 onPostExecute() 中取消它,然后执行在doInBackground()中下载。如果您想更新进度值,可以通过 doInBackground() 进行 publishProgress(),然后在 onProgressUpdate() 中的 ProgressBar 上进行更新>

https://github.com/ pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/TweetListActivity.java#L328
显示如何使用这些方法并显示/隐藏进度条( pg 变量)的 AsyncTask 示例

Actually I'd implement this with an AsyncTask, where you start the progress bar in onPreExecute(), cancel it in onPostExecute() and do the downloading in doInBackground(). If you want to update the progress value, you can publishProgress() from doInBackground() which can then be updated on the ProgressBar in onProgressUpdate()

https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/TweetListActivity.java#L328
shows how an example of an AsyncTask that uses those methods and which shows / hides a progress bar ( pg variable )

妥活 2024-10-22 10:09:31

问题是您没有恢复 myProgress 变量。在 onCreate() 的开头将其设置为 0:

@Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main1);
  myProgress = 0; // <--- notice that statement.
  ...
 }

另外,我建议您使用 AsyncTask 用于任何后台工作。请参阅此处的示例。

The problem is that you don't restore the myProgress variable. Set it to 0 in the beginning of onCreate():

@Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main1);
  myProgress = 0; // <--- notice that statement.
  ...
 }

Also, I would propose you to use AsyncTask for any background work. See example here.

萌面超妹 2024-10-22 10:09:31

您的代码看起来不像您希望的那样工作。您希望在该活动和下一个活动之间显示一个进度条(我的假设)。根据您的代码现在的工作方式,看起来当前 Activity 将启动一个线程,该线程将显示进度条约 100 秒,但同时您正在加载第二个 Activity。因此,如果在 100 秒结束之前,如果您点击后退按钮,您将返回到原始 Activity,该 Activity 仍显示进度条。

一般来说,我认为您不需要活动之间的进度条,因为它们应该非常顺利地在它们之间交换。我通常会使用进度条在活动完全呈现之前加载数据。

也许你应该看一下关于 ProgressBar 的一些 Android 文档:

http:// developer.android.com/reference/android/widget/ProgressBar.html

http://developer.android.com/reference/android/app/ProgressDialog.html

http://developer.android.com/guide/topics/ui/dialogs.html

Your code does not look like it is working as you want it to. You would like to display a progress bar between this Activity and the next (my assumption). Based on how your code is working now, it looks as though the current activity will start a thread that will display a progress bar for about 100 seconds, but at the same time you are loading your second Activity. So if before the 100 seconds is up, if you hit the back button, you will go back to the original Activity, which is still showing the progress bar.

Generally, I do not think that you need a progress bar between Activities though, as they should swap between them pretty smoothly. I would typically use a progress bar to load data before an Activity can be fully rendered.

Maybe you should take a look at some of the Android documentation on ProgressBar's:

http://developer.android.com/reference/android/widget/ProgressBar.html

http://developer.android.com/reference/android/app/ProgressDialog.html

http://developer.android.com/guide/topics/ui/dialogs.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文