如何播放简介,仅在安装应用程序时,单击按钮时播放?

问题描述 投票:0回答:2

在用户使用该应用程序之前,会有一个介绍,说明如何使用该应用程序。但是我想在主页上添加一个按钮,使用户能够再次看到该介绍。我应该怎么做?我已经尝试在课程首页中将课程介绍为Intro,但是什么也没发生这是我的简介代码:

    class Intro : AppCompatActivity() {

private val introSliderAdapter = IntroSliderAdapter(
    listOf(
        IntroSlide("Capture a ignição",
            "Tire uma fotografia á ignição que avista. Por favor mantanha o telemóvel na vertical",
            R.drawable.image1),
        IntroSlide("Localização",
            "É necessário que tenha o GPS ligado do seu telémovel. Ao capturar a fotografia serão automáticamente gravados a sua localização e o ângulo a que o fogo se encontra",
            R.drawable.image2),
        IntroSlide("Envio para os operacionais ",
            "Após os passos anteriores devidamente cumpridos, a informação fornecida será  avaliada por operacionais responsáveis.",
            R.drawable.image3)
    )
)

private var PRIVATE_MODE = 0
private val PREF_NAME = "mindorks-welcome"

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    //setContentView(R.layout.activity_main)
    val sharedPref: SharedPreferences = getSharedPreferences(PREF_NAME, PRIVATE_MODE)
    if (sharedPref.getBoolean(PREF_NAME, false)) {
        val homeIntent = Intent(this, HomePage::class.java)
        startActivity(homeIntent)
        finish()
    } else {
        setContentView(R.layout.activity_intro)
        playIntro()
        val editor = sharedPref.edit()
        editor.putBoolean(PREF_NAME, true)
        editor.apply()
    }
}

fun playIntro()
{
    introSliderViewPager.adapter=introSliderAdapter
    setupIndicators()
    setCurrentIndicator(0)
    introSliderViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
        override fun onPageSelected(position: Int) {
            setCurrentIndicator(position)

        }
    })


    buttonNext.setOnClickListener {
        if(introSliderViewPager.currentItem+1 < introSliderAdapter.itemCount){
            introSliderViewPager.currentItem+=1
        }else{

            Intent(applicationContext, HomePage::class.java).also {
                startActivity(it)
                finish()
            }

        }

    }
    buttonSkip.setOnClickListener {
        Intent(applicationContext, HomePage::class.java).also {
            startActivity(it)
            finish()
        }
    }

}
private fun setupIndicators(){
    val indicators = arrayOfNulls<ImageView>(introSliderAdapter.itemCount)
    val layoutParams : LinearLayout.LayoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    )
    layoutParams.setMargins(8,0,8,0)
    for (i in indicators.indices){
        indicators[i]  = ImageView(applicationContext)
        indicators[i].apply {
            this?.setImageDrawable(
                ContextCompat.getDrawable(
                    applicationContext,
                    R.drawable.indicator_inactive
                )
            )
            this?.layoutParams = layoutParams
        }
        indicatorsContainer.addView(indicators[i])
    }
}
private fun setCurrentIndicator(index:Int ){
    val childCount = indicatorsContainer.childCount
    for(i in 0 until childCount){
        val imageView = indicatorsContainer[i] as ImageView
        if(i==index){
            imageView.setImageDrawable(
                ContextCompat.getDrawable(
                    applicationContext,
                    R.drawable.indicator_active
                )
            )
            buttonNext.text ="PRÓXIMO"
        }else{
            imageView.setImageDrawable(
                ContextCompat.getDrawable(
                    applicationContext,
                    R.drawable.indicator_inactive
                )
            )
            buttonNext.text ="PRÓXIMO"


        }

    }
    if(index == introSliderAdapter.itemCount -1)
    {
        buttonNext.text ="FIM"
    }
}
 }
android-studio kotlin
2个回答
0
投票

也许您可以尝试将SharedPreference重置为false,然后再次调用Intro-Page。


0
投票

例如,将额外的Intent添加到您重新打开它的Intent中

intent.putExtra("FORCE", true)
startActivity(intent)

然后在您的简介活动中,检查额外内容和SharedPreference以确定是否完成:

if (sharedPref.getBoolean(PREF_NAME, false) && !intent.getExtra("FORCE", false)) {
    //...
© www.soinside.com 2019 - 2024. All rights reserved.