Kotlin如何在嵌套类中添加getSharedPreferences()方法

时间:2018-02-22 10:35:16

标签: kotlin kotlin-android-extensions

  1. 我试图在嵌套中添加getSharedPreferences()方法 类。我尝试了很多方法但未能添加此方法 theCustomSlide2类。请指导我如何打电话 CustomSlide2类中的getSharedPreferences()方法。我需要 从CustomSlide2 Class更新标志。

     package com.bangla.os.activity
    
     import agency.tango.materialintroscreen.MaterialIntroActivity
     import agency.tango.materialintroscreen.SlideFragment
     import agency.tango.materialintroscreen.SlideFragmentBuilder
     import agency.tango.materialintroscreen.widgets.OverScrollViewPager
     import android.content.Context
     import android.content.Intent
     import android.os.Bundle
     import android.support.v7.app.AppCompatActivity
     import android.view.LayoutInflater
     import android.view.View
     import android.view.ViewGroup
     import com.bangla.os.R
    
     class InitActivity : MaterialIntroActivity() {
    
         override fun onCreate(savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)
    
             if (!getSharedPreferences("quickSettings", Context.MODE_PRIVATE).getBoolean("firstStart", true)) {
                 skipStart()
                 return
             }
    
             val overScrollLayout = findViewById<View>(agency.tango.materialintroscreen.R.id.view_pager_slides) as OverScrollViewPager
             val viewPager = overScrollLayout.overScrollView
             viewPager.overScrollMode = View.OVER_SCROLL_NEVER
    
             addSlide(CustomSlide1())
             addSlide(CustomSlide2())
             addSlide(CustomSlide3())
    
         }
    
         override fun onFinish() {
           super.onFinish()
            setState()
         }
    
         private fun skipStart() {
            setState()
            finish()
         }
    
         private fun setState() {
            val intent = Intent(this@InitActivity, Home::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
            startActivity(intent)
         }
    
    class CustomSlide1 : SlideFragment() {
        override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
                val view = inflater.inflate(R.layout.intro_custom_layout, container, false)
                return view
         }
          override fun backgroundColor(): Int = R.color.op_blue
          override fun buttonsColor(): Int = R.color.intro_button_color3
     }
    
    
     class CustomSlide2 : SlideFragment() {
    
       getSharedPreferences("quickSettings", Context.MODE_PRIVATE).edit().putBoolean("firstStart", false).apply()
       override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
                val view = inflater.inflate(R.layout.intro_custom_layout_option, container, false)
                return view
        }
        override fun backgroundColor(): Int = R.color.op_blue
        override fun buttonsColor(): Int = R.color.intro_button_color3
     }
    
    
    
     class CustomSlide3 : SlideFragment() {
            override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
                  val view = inflater.inflate(R.layout.intro_custom_layout_confirm, container, false)
                  return view
            }
            override fun backgroundColor(): Int = R.color.op_blue
             override fun buttonsColor(): Int = R.color.intro_button_color3
       }
    
     }
    

1 个答案:

答案 0 :(得分:0)

你必须在函数内添加getSharedPreferences()语句。

类似的东西:

 class CustomSlide2 : SlideFragment() {

     override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
          val view = inflater.inflate(R.layout.intro_custom_layout_option, container, false)
          activity.getSharedPreferences("quickSettings", Context.MODE_PRIVATE).edit().putBoolean("firstStart", false).apply()
          return view
      }

      override fun backgroundColor(): Int = R.color.op_blue
      override fun buttonsColor(): Int = R.color.intro_button_color3
 }