如何将活动值传递给另一个活动(科特琳)

时间:2018-08-26 21:48:22

标签: android android-layout kotlin

我是Kotlin的新手,我正在尝试为我的应用创建设置活动,我希望该活动将其价值传递给其他活动。我尝试了不同的代码,但没有一个起作用,我尝试制作一个共享的首选项文件,但我不知道如何编写代码

要清楚,我想将字体类型从设置[main]活动传递到另一个活动,但是我不知道如何!

我的主要活动

val preferences = applicationContext.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)
    val prefEditor = preferences.edit()

    val fonts = arrayOf("Data1", "Data2", "Data3", "Data4")
    val adapterCountry = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fonts)
    val spinner = findViewById(R.id.spinner) as Spinner
    spinner.adapter = adapterCountry
    spinner.setSelection(preferences.getInt("position", 0))

    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
            spinner.setSelection(position)
            prefEditor.putInt("position", position)
            prefEditor.apply()
            val selecteditem = parent.getItemAtPosition(position).toString()
            if (selecteditem == "Data1"){

            }
        }

        override fun onNothingSelected(parent: AdapterView<*>) {
        }
    }

这是我主要的2个活动:

class Main2Activity : AppCompatActivity() {

internal lateinit var sh : SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main2)

    sh = PreferenceManager.getDefaultSharedPreferences(this)





}

override fun onStart() {
    super.onStart()
    if (sh.getBoolean("positon", false)){
        when(sh.getInt("position", 0)){
           0->{
               t1.typeface = Typeface.createFromAsset(assets, "andlso.ttf")
           }
            1->{
                t1.typeface = Typeface.createFromAsset(assets, "frsspbl")
            }
        }
    }


}
我在这里找到了这个问题的解决方案 enter link description here

2 个答案:

答案 0 :(得分:1)

Intent已具有将信息从一个活动发送到另一个活动的功能。

// in your first activity:

val intent = Intent(context, Main2Activity::class.java)
               .putExtra("position", position)
startActivity(intent)

// in your second activity, you can fetch the extras like this:

class Main2Activity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      val position = intent.getIntExtra("position", -1)

  }
}

我不建议使用共享的首选项在屏幕之间传递事件,因为如果您的应用在崩溃之前无法进入清理状态,则它们会陷入尴尬状态。

答案 1 :(得分:1)

因此,基本上,您希望将Int中的数据(字体类型)从一个活动传递到另一个活动。您可以使用SharedPrefenrences,但不建议在Android中使用。

我给您2种解决方案:

1。使用捆绑包

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val fonts = arrayOf("Data1", "Data2", "Data3", "Data4")
        val adapterCountry = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fonts)
        val spinner = findViewById(R.id.spinner) as Spinner
        spinner.adapter = adapterCountry

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                spinner.setSelection(position)
                val selecteditem = parent.getItemAtPosition(position).toString()
                if (selecteditem == "Data1") {

                }

                // Start another activity with position
                val intent = Intent(this@MainActivity, Main2Activity::class.java)
                intent.putExtra("position", position);
                startActivity(intent)
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
            }
        }
    }
}

Main2Activity.kt

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val position = intent.getIntExtra("position", 0)
    }
}

2。使用SharedPreferences

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val preferences = PreferenceManager.getDefaultSharedPreferences(this)

        val fonts = arrayOf("Data1", "Data2", "Data3", "Data4")
        val adapterCountry = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fonts)
        val spinner = findViewById(R.id.spinner) as Spinner
        spinner.adapter = adapterCountry
        spinner.setSelection(preferences.getInt("position", 0))

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                spinner.setSelection(position)
                val selecteditem = parent.getItemAtPosition(position).toString()
                if (selecteditem == "Data1"){

                }

                // Save position to prefs.
                preferences.edit()
                        .putInt("position", position)
                        .apply()
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
            }
        }
    }
}

Main2Activity.kt

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Get position from prefs.
        val position = PreferenceManager.getDefaultSharedPreferences(this).getInt("position", 0)
    }
}