Andloid Studio 3.6.3 Spinner等の選択値の保存方法|teratail

Andloid Studio 3.6.3  Spinner等の選択値の保存方法|teratail

Android Studioを初めて使用する初心者です。サンプルプログラムからSpinner, seekBar、RadioButton、RadioButtonをコードイングしてその値をheight.texに表示することを確認できましたが、アプリ再起動時に選択した値がheight.

Android Studioを初めて使用する初心者です。

サンプルプログラムからSpinner, seekBar、RadioButton、RadioButtonをコードイングしてその値をheight.texに表示することを確認できましたが、アプリ再起動時に選択した値がheight.texには反映しますがSpinner, seekBar、RadioButtonそれぞれにも設定値が反映されるようにしたいです。

ネット上でいくつかの参考例がありましたが理解できずにいます。

下記のサンプルコードで再起動時に設定値ががSpinner, seekBar、RadioButtonに反映されるにはどのようなコーデイングが必要なのでしょうか方法を教えていただきたく宜しくお願い致します。

Andoroid Studio(kotlin対応)

-------------------------------------HeightActivity.xt

package com.example.mysize

import android.os.Bundle

import android.preference.PreferenceManager

import android.view.View

import android.widget.AdapterView

import android.widget.RadioButton

import android.widget.SeekBar

import android.widget.Spinner

import androidx.appcompat.app.AppCompatActivity

import androidx.core.content.edit

import kotlinx.android.synthetic.main.activity_height.*

class HeightActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_height)

//Spinner------

spinner5.onItemSelectedListener=

object : AdapterView.OnItemSelectedListener{

override

fun onItemSelected(

parent: AdapterView<*>?,

view: View?,

position: Int,

id: Long

) {

val spinner5 = parent as? Spinner

val item = spinner5?.selectedItem as? String

item?.let {

if (it.isNotEmpty()) height.text = it

}

}

override

fun onNothingSelected(parent: AdapterView<*>?) {

}

}

val pref = PreferenceManager.getDefaultSharedPreferences(this)

val heightVal = pref.getInt("HEIGHT", 160)

height.text = heightVal.toString()

seekBar.progress = heightVal

//seekBar-------

seekBar.setOnSeekBarChangeListener(

object : SeekBar.OnSeekBarChangeListener {

override fun onProgressChanged(

seekBar: SeekBar?,

progress: Int,

fromUser: Boolean

) {

height.text = progress.toString()

}

override

fun onStartTrackingTouch(seekBar: SeekBar?) {

}

override

fun onStopTrackingTouch(seekBar: SeekBar?) {

}

})

//RadioButton------

radioGroup.setOnCheckedChangeListener { group, checkedId ->

height.text = findViewById(checkedId).text

}

}

override fun onPause() {

super.onPause()

val pref = PreferenceManager.getDefaultSharedPreferences(this)

pref.edit {

//保存してる

putInt("HEIGHT", height.text.toString().toInt())

}

}

}

code teratail spinner

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

How to Find the Stinky Parts of Your Code (Part II)

There are more code smells. Let’s keep changing the aromas. We see several symptoms and situations that make us doubt the quality of our development. Let's look at some possible solutions.

Let's Talk About Selling Your Code

In this video, I'll be talking about when do I think code is ready to be sold. 🔴 Subscribe for more https://www.youtube.com/channel/UCMA8gVyu_IkVIixXd2p18NQ?...

Softagram - Making Code Reviews Humane

The story of Softagram is a long one and has many twists. Everything started in a small company long time ago, from the area of static analysis tools development. After many phases, Softagram is focusing on helping developers to get visual feedback on the code change: how is the software design evolving in the pull request under review.

Embold Is Like Autocorrect For Code, Says Vishal Rai, Founder & CEO

We, at Analytics India Magazine, spoke to founder and CEO, Vishal Rai, to understand how Embold can detect anti-patterns in code for seamless integration. Embold started a decade ago, with the vision of creating a product that can revolutionise the way developers write and design code.