外包网站都有哪些,关键词优化下拉管家,wordpress翻页404,网络营销的基本方式我是Kotlin的新手#xff0c;我正在尝试为我的应用进行设置活动#xff0c;我希望该活动将其价值传递给其他活动。 我尝试了不同的代码#xff0c;但没有一个起作用#xff0c;我尝试制作一个共享的首选项文件#xff0c;但我不知道如何编写代码要清楚#xff0c;我想将字…我是Kotlin的新手我正在尝试为我的应用进行设置活动我希望该活动将其价值传递给其他活动。 我尝试了不同的代码但没有一个起作用我尝试制作一个共享的首选项文件但我不知道如何编写代码要清楚我想将字体类型从设置[main]活动传递到另一个活动但我不知道如何我的主要活动val preferences applicationContext.getSharedPreferences(MyPrefs, Context.MODE_PRIVATE)val prefEditor preferences.edit()val fonts arrayOf(Data1,Data2,Data3,Data4)val adapterCountry ArrayAdapter(this, android.R.layout.simple_list_item_1, fonts)val spinner findViewById(R.id.spinner) as Spinnerspinner.adapter adapterCountryspinner.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 : SharedPreferencesoverride 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)}}}}我在这里找到了这个问题的解决方案在此处输入链接说明尝试将此回答改写为Kotlin。如果只是从那个活动到另一个活动而已链接在一起则可以考虑仅传递一个意图。 否则如果您确实需要在应用程序范围内使用首选项是个好方法developer.android.com/training/data-storage/shared-preferences因此基本上您想将Int中的数据(字体类型)从一个活动传递到另一个活动。 您可以使用SharedPrefenrences但在Android中不建议使用。我给您2个解决方案1.使用捆绑MainActivity.ktclass 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(this, android.R.layout.simple_list_item_1, fonts)val spinner findViewById(R.id.spinner) as Spinnerspinner.adapter adapterCountryspinner.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 positionval intent Intent(thisMainActivity, Main2Activity::class.java)intent.putExtra(position, position);startActivity(intent)}override fun onNothingSelected(parent: AdapterView) {}}}}Main2Activity.ktclass Main2Activity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val position intent.getIntExtra(position, 0)}}2.使用SharedPreferencesMainActivity.ktclass 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(this, android.R.layout.simple_list_item_1, fonts)val spinner findViewById(R.id.spinner) as Spinnerspinner.adapter adapterCountryspinner.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.ktclass Main2Activity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)// Get position from prefs.val position PreferenceManager.getDefaultSharedPreferences(this).getInt(position, 0)}}您能告诉我如何在其他屏幕上获取数据吗 在这种情况下只需使用intent.getIntExtra(position, 0)或intent.getIntExtra(position1, 0)。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)}}我不建议使用共享首选项在屏幕之间传递事件因为如果您的应用在崩溃之前无法清理就可能陷入尴尬的状态从而无法清除其共享的偏好状态。它说上下文没有分类器