简体   繁体   English

如何使用单选按钮更改 android 应用程序中的设置?

[英]How change setting in android app using radiobutton?

im developing weather app and its almost done but there is problem.我正在开发天气应用程序,它几乎完成了,但有问题。

i create an Activity called "setting".我创建了一个名为“设置”的活动。 in that activity i create a group radiobutton To give the user the option to change the temperature unit from C(centigrade) to F(fahrenheit).but i have no idea to how to do it?在那个活动中,我创建了一个组单选按钮,让用户可以选择将温度单位从 C(摄氏度)更改为 F(华氏度)。但我不知道该怎么做?

i want when user choose F every temperature units change to F.我希望当用户选择 F 时,每个温度单位都更改为 F。

This my Radio group button:这是我的单选组按钮:

在此处输入图像描述

Save the users choice using SharedPreferences and use it to display the data accordingly.使用SharedPreferences保存用户选择并使用它来相应地显示数据。

Settings Activity设置活动

private var prefs =
        this.getSharedPreferences("com.example.android.appname", Context.MODE_PRIVATE)

  radioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->
        val userChoice = when(checkedId){
              R.id.radio1 -> "centigrade"
              R.id.radio2 -> "fahrenheit"
        }

      prefs.edit().putString("temparatureMode", userChoice).apply()
  })
// assuming you want to display the data in text field

private var prefs =
        this.getSharedPreferences("com.example.android.appname", Context.MODE_PRIVATE)

val selectedChoice = prefs.getString("temparatureMode", "centigrade")  // assuming centigrade is default mode

val textfield1 = findViewById<TextView>(R.id.textfield1)

textfield1.text = when(selectedChoice){
"centigrade" -> // logic based on centigrade choice
"fahrenheit" -> // logic based on fahrenheit choice
}

check the docs for more info检查文档以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM