创建进度条

时间:2014-01-13 05:18:40

标签: android

我创建了一个android程序。现在我想要一个带有我的应用程序名称的屏幕和一个带有加载符号的进度条。进度条应该等待3秒然后我的程序必须加载。我怎么能实现这个?

2 个答案:

答案 0 :(得分:1)

您要实施的内容称为Splashscreen。为此:

创建活动SplashScreenActivity.java

public class SplashScreenActivity extends Activity {

ProgressDialog mProgressDialog;

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

    mProgressDialog = ProgressDialog.show(this, 
                     "Loading", "Please wait...", true);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            mProgressDialog.dismiss();
            startActivity(new Intent(SplashScreenActivity.this, SecondActivity.class));
            finish();
        }
    }, 3000);
}
}

然后,创建另一个活动SecondActivity.java。将您的SplashScreenActivity.java作为启动器活动,然后运行您的项目。

修改

要将您的活动作为启动器活动,只需在相关活动代码中添加以下行:

<intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

答案 1 :(得分:0)

<强> pgbar.xml:

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

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

<强> pgbar.java

public class SplashScreen extends Activity {

private static int SPLASH_TIME_OUT = 3000;
ProgressBar mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pgbar);

     mProgress = (ProgressBar) findViewById(R.id.progressBar1);
     mProgress .setCancelable(true);
     mProgress .setMessage("Loading Please wait ...");
     mProgress .setProgress(0);
     mProgress .setMax(100);
     mProgress .show();


    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            Intent i = new Intent(pgbar.this, NextActivity.class);
            startActivity(i);
            finish();
        }
    }, SPLASH_TIME_OUT);
}

}