赞
踩
一、新建一个启动页Activity(Main2Activity .java),代码如下
- package com.bignerdranch.android.myapplication;
-
- import android.content.Intent;
- import android.os.Handler;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.TextView;
-
- import java.util.Timer;
- import java.util.TimerTask;
-
- public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
-
- private int recLen = 5;//跳过倒计时提示5秒
- private TextView tv;
- Timer timer = new Timer();
- private Handler handler;
- private Runnable runnable;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //定义全屏参数
- int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
- //设置全屏
- getWindow().setFlags(flag,flag);
- //隐藏标题栏
- getSupportActionBar().hide();
- setContentView(R.layout.activity_main2);
- initView();
- //等待一秒,停留一秒
- timer.schedule(task,1000,1000);
-
- handler = new Handler();
- handler.postDelayed(runnable = new Runnable() {
- @Override
- public void run() {
- //从启动界面跳转到主界面
- Intent intent = new Intent(Main2Activity.this,MainActivity.class);
- startActivity(intent);
- finish();
- }
- //延时5s发送handler信息
- },5000);
- }
-
- private void initView() {
- //跳过监听
- tv = findViewById(R.id.textView2);
- tv.setOnClickListener(this);
- }
-
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- recLen--;
- tv.setText("跳过 "+recLen );
- if (recLen < 0){
- timer.cancel();
- tv.setVisibility(View.GONE);
- }
- }
- });
- }
- };
- //点击跳过
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case R.id.textView2:
- Intent intent = new Intent(Main2Activity.this,MainActivity.class);
- startActivity(intent);
- finish();
- if (runnable != null){
- handler.removeCallbacks(runnable);
- }
- break;
- default:
- break;
- }
- }
- }
二、新建一个对应的布局文件(activity_main2.xml),这个布局中包括一个跳过按钮,一个图片,一个textView,可随意添加,在此之前记得先在drawable文件夹下添加需要展示的启动页图片。
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".Main2Activity">
-
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:contentDescription="@string/todo"
- app:layout_constraintBottom_toTopOf="@+id/textView"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="1.0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="1.0"
- app:srcCompat="@drawable/test" />
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginBottom="8dp"
- android:text="@string/coolapk"
- android:textSize="24sp"
- android:textStyle="bold"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/imageView" />
-
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="16dp"
- android:layout_marginEnd="16dp"
- android:text="TextView"
- android:background="@drawable/shape"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
三、修改AndroidMainifest.xml,先启动启动页Activity,将主Activity中的intent-filter标签移到启动页Activity下。
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.bignerdranch.android.myapplication">
-
- <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/AppTheme">
- <activity android:name=".MainActivity">
-
- </activity>
- <activity android:name=".Main2Activity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </manifest>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。