Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android studio 如何播放简介,只有在安装了应用程序、点击按钮时才会播放?_Android Studio_Kotlin - Fatal编程技术网

Android studio 如何播放简介,只有在安装了应用程序、点击按钮时才会播放?

Android studio 如何播放简介,只有在安装了应用程序、点击按钮时才会播放?,android-studio,kotlin,Android Studio,Kotlin,在用户使用应用程序之前,有一个介绍如何使用应用程序的介绍。但是,我想在主页上添加一个按钮,让用户能够再次看到介绍。我该怎么做? 我已经试着在班级主页上打电话给班级介绍,但什么也没发生 以下是我介绍的代码: class Intro : AppCompatActivity() { private val introSliderAdapter = IntroSliderAdapter( listOf( IntroSlide("Capture a ignição",

在用户使用应用程序之前,有一个介绍如何使用应用程序的介绍。但是,我想在主页上添加一个按钮,让用户能够再次看到介绍。我该怎么做? 我已经试着在班级主页上打电话给班级介绍,但什么也没发生 以下是我介绍的代码:

    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"
    }
}
 }

您可以尝试将SharedReference重置为false,然后再次调用简介页面。

例如,在重新打开它的意图中添加一个额外的意图

intent.putExtra("FORCE", true)
startActivity(intent)
然后在介绍活动中,检查额外参考和共享参考,以决定是否完成:

if (sharedPref.getBoolean(PREF_NAME, false) && !intent.getExtra("FORCE", false)) {
    //...