当前位置:   article > 正文

Android启动页及其相关_安卓activity_main可以当启动页吗

安卓activity_main可以当启动页吗

一、新建一个启动页Activity(Main2Activity .java),代码如下

  1. package com.bignerdranch.android.myapplication;
  2. import android.content.Intent;
  3. import android.os.Handler;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.view.WindowManager;
  8. import android.widget.TextView;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11. public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
  12. private int recLen = 5;//跳过倒计时提示5秒
  13. private TextView tv;
  14. Timer timer = new Timer();
  15. private Handler handler;
  16. private Runnable runnable;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. //定义全屏参数
  21. int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
  22. //设置全屏
  23. getWindow().setFlags(flag,flag);
  24. //隐藏标题栏
  25. getSupportActionBar().hide();
  26. setContentView(R.layout.activity_main2);
  27. initView();
  28. //等待一秒,停留一秒
  29. timer.schedule(task,1000,1000);
  30. handler = new Handler();
  31. handler.postDelayed(runnable = new Runnable() {
  32. @Override
  33. public void run() {
  34. //从启动界面跳转到主界面
  35. Intent intent = new Intent(Main2Activity.this,MainActivity.class);
  36. startActivity(intent);
  37. finish();
  38. }
  39. //延时5s发送handler信息
  40. },5000);
  41. }
  42. private void initView() {
  43. //跳过监听
  44. tv = findViewById(R.id.textView2);
  45. tv.setOnClickListener(this);
  46. }
  47. TimerTask task = new TimerTask() {
  48. @Override
  49. public void run() {
  50. runOnUiThread(new Runnable() {
  51. @Override
  52. public void run() {
  53. recLen--;
  54. tv.setText("跳过 "+recLen );
  55. if (recLen < 0){
  56. timer.cancel();
  57. tv.setVisibility(View.GONE);
  58. }
  59. }
  60. });
  61. }
  62. };
  63. //点击跳过
  64. @Override
  65. public void onClick(View v) {
  66. switch (v.getId()){
  67. case R.id.textView2:
  68. Intent intent = new Intent(Main2Activity.this,MainActivity.class);
  69. startActivity(intent);
  70. finish();
  71. if (runnable != null){
  72. handler.removeCallbacks(runnable);
  73. }
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. }

二、新建一个对应的布局文件(activity_main2.xml),这个布局中包括一个跳过按钮,一个图片,一个textView,可随意添加,在此之前记得先在drawable文件夹下添加需要展示的启动页图片。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".Main2Activity">
  8. <ImageView
  9. android:id="@+id/imageView"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_marginBottom="8dp"
  13. android:contentDescription="@string/todo"
  14. app:layout_constraintBottom_toTopOf="@+id/textView"
  15. app:layout_constraintEnd_toEndOf="parent"
  16. app:layout_constraintHorizontal_bias="1.0"
  17. app:layout_constraintStart_toStartOf="parent"
  18. app:layout_constraintTop_toTopOf="parent"
  19. app:layout_constraintVertical_bias="1.0"
  20. app:srcCompat="@drawable/test" />
  21. <TextView
  22. android:id="@+id/textView"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_marginStart="8dp"
  26. android:layout_marginEnd="8dp"
  27. android:layout_marginBottom="8dp"
  28. android:text="@string/coolapk"
  29. android:textSize="24sp"
  30. android:textStyle="bold"
  31. app:layout_constraintBottom_toBottomOf="parent"
  32. app:layout_constraintEnd_toEndOf="parent"
  33. app:layout_constraintStart_toStartOf="parent"
  34. app:layout_constraintTop_toBottomOf="@+id/imageView" />
  35. <TextView
  36. android:id="@+id/textView2"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_marginTop="16dp"
  40. android:layout_marginEnd="16dp"
  41. android:text="TextView"
  42. android:background="@drawable/shape"
  43. app:layout_constraintEnd_toEndOf="parent"
  44. app:layout_constraintTop_toTopOf="parent" />
  45. </android.support.constraint.ConstraintLayout>

三、修改AndroidMainifest.xml,先启动启动页Activity,将主Activity中的intent-filter标签移到启动页Activity下。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.bignerdranch.android.myapplication">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. </activity>
  13. <activity android:name=".Main2Activity">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. </application>
  20. </manifest>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/126367
推荐阅读
相关标签
  

闽ICP备14008679号