进度条问题
我有一个按钮,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,我会使用
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 inonPreExecute()
, cancel it inonPostExecute()
and do the downloading indoInBackground()
. If you want to update the progress value, you canpublishProgress()
fromdoInBackground()
which can then be updated on the ProgressBar inonProgressUpdate()
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 )问题是您没有恢复 myProgress 变量。在 onCreate() 的开头将其设置为 0:
另外,我建议您使用 AsyncTask 用于任何后台工作。请参阅此处的示例。
The problem is that you don't restore the myProgress variable. Set it to 0 in the beginning of onCreate():
Also, I would propose you to use AsyncTask for any background work. See example here.
您的代码看起来不像您希望的那样工作。您希望在该活动和下一个活动之间显示一个进度条(我的假设)。根据您的代码现在的工作方式,看起来当前 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