Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
46 views
in Technique[技术] by (71.8m points)

java - 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?

i want when user choose F every temperature units change to F.

This my Radio group button:

enter image description here

question from:https://stackoverflow.com/questions/65933672/how-change-setting-in-android-app-using-radiobutton

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Save the users choice using SharedPreferences and use it to display the data accordingly.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...