首页 > 解决方案 > 如何在 kotlin 的介绍之前出现启动画面?

问题描述

我有一个应用程序,首次安装时会出现启动画面,然后会出现一个包含 3 个片段的介绍,并解释该应用程序的工作原理。然而,一旦用户走过那个“介绍”,只会出现启动画面。我该怎么做?我相信这必须做我的清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pfinal">
<!-- Permissoes para o acesso a camera -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- Permissoes para o acesso ao GPS -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_fire_round"
    android:label="AppFogos"
    android:roundIcon="@mipmap/ic_fire_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Intro">
     <intent-filter>
         <action android:name="android.intent.action.MAIN" />

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

 </activity>
 <!-- The below is for the splash screen and we need no action bar and the default theme -->
    <activity
        android:name=".SplashScreen"
        android:theme="@style/AppTheme.NoActionBar">
        <!-- <intent-filter>
             <action android:name="android.intent.action.MAIN" />

             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>-->
     </activity>
     <activity android:name=".AppFogos" />
     <activity android:name=".HomePage">
         <!-- <intent-filter>
              <action android:name="android.intent.action.MAIN" />
         </intent-filter>-->
    </activity>
</application>

Intro 是带有walktrough 的活动,SplashScreen 是带有闪屏的活动。现在,只出现简介。

这是“介绍”的代码:

val fragment1 = SliderFragment()
val fragment2 = SliderFragment()
val fragment3 = SliderFragment()
lateinit var adapter : myPagerAdapter
lateinit var activity: Activity
lateinit var preference : SharedPreferences
val pref_show_intro = "Intro"
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_intro)
    activity = this
    preference = getSharedPreferences("~PFinal", Context.MODE_PRIVATE)

    if(!preference.getBoolean(pref_show_intro,true)){
        startActivity(Intent(activity,SplashScreen::class.java))
        finish()

    }
    fragment1.setTitle("welcome")
    fragment2.setTitle("fogos")
    fragment3.setTitle("o que utilizamos")

    adapter = myPagerAdapter(supportFragmentManager)
    adapter.list.add(fragment1)
    adapter.list.add(fragment2)
    adapter.list.add(fragment3)


    view_pager.adapter  = adapter
    next.setOnClickListener {
        view_pager.currentItem++
    }
    skip.setOnClickListener { goToHomePage() }
    view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
        override fun onPageScrollStateChanged(state: Int) {

        }

        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {

        }

        override fun onPageSelected(position: Int) {
            if(position == adapter.list.size-1)
            {
                //lastpage
                next.text="DONE"
                next.setOnClickListener {
                    goToHomePage()

                }
            }else{
                next.text="NEXT"
                next.setOnClickListener {
                    view_pager.currentItem++
                }
            }

            when(view_pager.currentItem)
            {
                0->{
                    indicator1.setTextColor(Color.BLACK)
                    indicator2.setTextColor(Color.GRAY)
                    indicator3.setTextColor(Color.GRAY)
                }
                1->{
                    indicator1.setTextColor(Color.GRAY)
                    indicator2.setTextColor(Color.BLACK)
                    indicator3.setTextColor(Color.GRAY)

                }
                2->{
                    indicator1.setTextColor(Color.GRAY)
                    indicator2.setTextColor(Color.GRAY)
                    indicator3.setTextColor(Color.BLACK)
                }
            }
        }


    })
}
fun goToHomePage(){
    startActivity(Intent(activity,HomePage::class.java))
    finish()
    val editor = preference.edit()
    editor.putBoolean(pref_show_intro,false)
    editor.apply()
}

class myPagerAdapter(manager : FragmentManager): FragmentPagerAdapter(manager){
    val list : MutableList<Fragment> = ArrayList()

    override fun getItem(position: Int): Fragment {
        return list[position]
    }

    override fun getCount(): Int {
        return list.size
    }


}

标签: androidxmlandroid-studiokotlin

解决方案


推荐阅读