Android project actual battle welcome interface

Posted by PierceCoding on Mon, 02 Dec 2019 18:50:27 +0100

Welcome interface
First of all, import the picture of the welcome interface into the drawable directory. When importing, Android Studio will prompt as follows

drawable

I haven't figured out the details yet. I will fill in this part again after understanding. Here I choose the first one

Then create a package named activity, and create SplashActivity under activity

SplashActivity

You will find that R reports red. Just lock the cursor to R and you will be prompted to import R package. If not, click Alt+Enter manually and select import class to import

R red

And then deal with the code part

SplashActivity
package cn.edu.lt.android.boxueguapp.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import cn.edu.lt.android.boxueguapp.MainActivity;
import cn.edu.lt.android.boxueguapp.R;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //Set this interface as
        // Vertical screen
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
    }

    private void init() {
        TextView tv_version = (TextView)findViewById(R.id.tv_version);
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            tv_version.setText("V" + packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            tv_version.setText("V");
        }

        //Use timer to delay the interface for 3 seconds and then jump. Timer has a thread that continuously executes task s
        Timer timer = new Timer();
        //TimerTask implements the runnable interface, and the TimerTask class represents the task executed in a specified time
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {//Send intent to realize page Jump. The first parameter is the context of the current page, and the second parameter is the homepage to jump
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                SplashActivity.this.finish();//Close the current welcome page after jump
            }
        };
        timer.schedule(timerTask,3000);//Scheduled execution timerTask, second parameter incoming delay time (MS)

    }
}

When creating an Activity, a layout file is automatically created. First, modify it to RelativeLayout layout

The specific code is as follows:

activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width = "match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/launch_bg">
    <TextView
        android:id="@+id/tv_version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="14sp"
        android:layout_centerInParent="true"/><!--Display version number-->

</RelativeLayout>
//Finally, configure the welcome interface in the list file Android manifest.xml, change the application entry to the welcome interface, and then remove the ActionBar effect

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.lt.android.boxueguapp">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <!--originally be android:theme="@style/AppTheme"--><!--Remove ActionBar Title Bar-->
    <activity android:name=".MainActivity">
    </activity>
    <activity android:name=".activity.SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

Topics: Android xml Java encoding