-1

填充进度条后,我需要显示第二个活动。我尝试了下面的代码,但它没有显示进度条,只显示了我的第二个活动。

这是代码:

   public class MiSuper2 extends Activity {
        String strListas[] = null;
        private ProgressBar mProgress;
        private int mProgressStatus = 0;
        private Handler mHandler = new Handler();
        private StoreData stdArticulos = null;
        public Cursor cursor = null;
        private long fileSize = 0;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        stdArticulos = new StoreData(this);

        fileSize = 0;
        setContentView(R.layout.main);
        stdArticulos = new StoreData(this);
        cursor = stdArticulos.leerArticulos();

        mProgress = (ProgressBar) findViewById(R.id.progressbar_activity);

        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < 100) {
                    mProgressStatus = doWork();

                    mHandler.post(new Runnable() {
                        public void run() {
                            mProgress.setProgress(mProgressStatus);
                        }
                    });
                }
            }
        }).start();

        if(cursor.moveToFirst()) {
            do{
                strListas[cursor.getPosition()] = cursor.getString(cursor.getPosition());
            }while(cursor.moveToNext());
        }       

        Intent intent = new Intent(MiSuper2.this, PntArticulo.class);
        startActivity(intent);

    }

    public int doWork() {       
        while (fileSize <= 1000000) {
            fileSize++;
            return (int) fileSize;
        }
        return 100;
    }
}

这是 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imvLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/presentacion" 
        android:contentDescription="@string/logo"/>

    <ProgressBar
        android:id="@+id/progressbar_activity"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dp"
        android:layout_height="wrap_content" android:layout_marginTop="100dp"/>

</LinearLayout>

请帮忙

4

2 回答 2

2

看起来您正在使用该doWork()功能来占用时间,以便进度条执行某些操作。即使您编写了一个大循环,它仍然执行得非常快,因此您看不到进度条移动。相反,你想模拟你的线程做一些计算密集型的事情,使用Thread.sleep()which 接受一个参数,即睡眠时间(以毫秒为单位)。

尝试将您的代码更改为:

new Thread(new Runnable() {
    public void run() {

        while (mProgressStatus < 100) {
            try {
                mProgressStatus += doWork();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            mHandler.post(new Runnable() {
                public void run() {
                    mProgress.setProgress(mProgressStatus);
                }
            });
        }

        runOnUiThread(new Runnable() {
            @Override
                public void run() {
                    startActivity(new Intent(MiSuper2.this, Second.class));
                }
            });
        }
}).start();

和...

public int doWork() throws InterruptedException {
    Thread.sleep(1000);
    return 1;
}

这将使您的进度条每秒增加 1%。最后,文档Thread.sleep()https ://developer.android.com/reference/java/lang/Thread.html#sleep(long )

编辑:Ramz 在这个答案上击败了我,但没有解释为什么它是答案。希望我的解释有所帮助。

EDIT2:我认为你编辑了你的问题,因为我开始第二次看它。以前您的 XML 中有一些错误,但现在它已经消失了。无论如何,您现在的问题是您需要在startActivity()工作线程内部调用。否则,UI 线程不会等待doWork()函数返回,而是在您的应用启动时立即启动另一个 Activity。对不起,我应该在之前提到这一点。我在上面发布的代码随此更改而更新。

于 2012-05-14T21:33:39.043 回答
1

请尝试此代码 SplashScreen.java

package com.cud.point;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;

public class SplashScreen extends Activity {

 ProgressBar bar;

 TextView txt;

 int total=0;

 boolean isRunning=false;

 // handler for the background updating

 Handler handler=new Handler() {

 @Override

 public void handleMessage(Message msg) {

 total=total+20;

 String perc=String.valueOf(total).toString();

 txt.setText(perc+"% completed");

 bar.incrementProgressBy(20);

 }

 };

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.splash);

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

 txt=(TextView)findViewById(R.id.txt);
 Handler x = new Handler();
 x.postDelayed(new SplashHandler(), 5000);
 }
 class SplashHandler implements Runnable 
 {

    public void run() {
        // TODO Auto-generated method stub
        startActivity(new Intent(getApplication(),YourSecound Activity.class));
        SplashScreen.this.finish();
    }


}

public void onStart() {

 super.onStart();

 // reset the bar to the default value of 0

 bar.setProgress(0);

  // create a thread for updating the progress bar

 Thread background=new Thread(new Runnable() {

 public void run() {

 try {

 for (int i=0;i<5 && isRunning;i++) {

 // wait 100ms between each update

 Thread.sleep(1000);

 handler.sendMessage(handler.obtainMessage());

 }

 }

 catch (Throwable t) {

   }     }     });

 isRunning=true;

  // start the background thread

 background.start();

 }

 public void onStop() {

 super.onStop();

 isRunning=false;

 }

}

飞溅.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="15px" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Loading......" />

<TextView
    android:id="@+id/txt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_below="@+id/imageView1"/>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/splash" />

<ProgressBar
    android:id="@+id/progress"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/imageView1"
    android:max="100" />

</RelativeLayout>

这是我的项目的一个例子,所以请在 xml 文件中进行必要的更改

于 2012-05-14T21:27:57.580 回答