在Android Studio中保存实例状态:Kotlin

huangapple go评论68阅读模式
英文:

save Instance State in Android studio: Kotlin

问题

MainActivity.kt:

class MainActivity : AppCompatActivity() {
    
    private var count = 10
    private lateinit var textCount: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        textCount = findViewById(R.id.textView)
        
        val buttonRed = findViewById<Button>(R.id.injury)
        val buttonIng = findViewById<Button>(R.id.vial)

        buttonRed.setOnClickListener {
            if (count >= 0) {
                count--
                textCount.text = count.toString()
            }
        }

        buttonIng.setOnClickListener {
            if (count <= 10)
                count += 3
            textCount.text = count.toString()
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("count", count)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        count = savedInstanceState.getInt("count")
        textCount.text = count.toString()
    }
}

XML Code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="60dp"
        android:layout_height="78dp"
        android:layout_marginStart="145dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="145dp"
        android:textSize="50sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/injury"
        android:layout_width="106dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="150dp"
        android:text="injury" />
    <Button
        android:id="@+id/vial"
        android:layout_width="197dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="109dp"
        android:text="vial" />
</LinearLayout>

请注意,我在代码中进行了一些修正,包括:

  1. findViewById 的泛型参数从 View 更改为正确的类型,例如 ButtonTextView
  2. MainActivity 类中添加了 onSaveInstanceStateonRestoreInstanceState 方法,以便保存和恢复 count 变量的状态。
  3. 在 XML 中,将 android:textSize 的单位从 dp 更改为 sp,因为在文本视图中,推荐使用 sp 作为文本大小的单位,以便在不同设备上更好地适应文本大小。

以上是您提供的代码的翻译和修改部分。

英文:

I was doing this program and tried to run an app as an output. However, when I turned the app in Landscape mode, my text output didn't work. I realized that I need to add onSaveInstanceState in the Kotlin code, which I don't know how to do.

MainActivity.kt:

class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        var count = 10
        val textCount = findViewById&lt;View&gt;(R.id.textView) as TextView
        val buttonred = findViewById&lt;View&gt;(R.id.injury) as Button
        val buttoning = findViewById&lt;View&gt;(R.id.vial) as Button

        buttonred.setOnClickListener {
            if (count &gt;= 0) {
                count--
                textCount.text = count.toString()
            }
        }

        buttoning.setOnClickListener {
            if (count &lt;= 10)
                count += 3
            textCount.text = count.toString()
        }
    }
}

XML Code

LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

&lt;TextView
        android:id=&quot;@+id/textView&quot;
        android:layout_width=&quot;60dp&quot;
        android:layout_height=&quot;78dp&quot;
        android:layout_marginStart=&quot;145dp&quot;
        android:layout_marginTop=&quot;68dp&quot;
        android:layout_marginEnd=&quot;145dp&quot;
        android:textSize=&quot;50dp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;Button
        android:id=&quot;@+id/injury&quot;
        android:layout_width=&quot;106dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginHorizontal=&quot;150dp&quot;
        android:text=&quot;injury&quot;
         /&gt;
&lt;Button
        android:id=&quot;@+id/vial&quot;
        android:layout_width=&quot;197dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginHorizontal=&quot;109dp&quot;
        android:text=&quot;vial&quot; /&gt;
&lt;/LinearLayout&gt;

I would like to know how to add the onSaveInstanceState

答案1

得分: 3

以下是翻译好的代码部分:

MainActivity.kt:

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    companion object {
        const val COUNT_KEY = "COUNT_KEY" // 保存/读取 bundle 值的常量键
    }
    
    private var count = 0 // 带有setter的计数值。这样更方便,您可以更改该值,无需考虑设置 TextView.text
        set(value) {
            field = value
            txtCount.text = value.toString()
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.i("MyTag", "onCreate")

        txtCount.text = count.toString()

        butIncrement.setOnClickListener {
            count++
        }

    }

    override fun onSaveInstanceState(outState: Bundle) { // 在这里保存计数值
        super.onSaveInstanceState(outState)
        Log.i("MyTag", "onSaveInstanceState")
        
        outState.putInt(COUNT_KEY, count)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) { // 在这里还原计数值
        super.onRestoreInstanceState(savedInstanceState)
        Log.i("MyTag", "onRestoreInstanceState")

        count = savedInstanceState.getInt(COUNT_KEY)
    }
}

activity_main.xml:

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/butIncrement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Increment" />

    <TextView
        android:id="@+id/txtCount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="40sp" />

</androidx.appcompat.widget.LinearLayoutCompat>

MainActivity.kt(编辑后的代码):

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    lateinit var textCount: TextView
    lateinit var buttonRed: Button
    lateinit var buttonIncrement: Button

    companion object {
        const val COUNT_KEY = "COUNT_KEY"
    }

    private var count = 0
        set(value) {
            field = value
            textCount.text = value.toString()
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.i("MyTag", "onCreate")

        textCount = findViewById<View>(R.id.textView) as TextView
        buttonRed = findViewById<View>(R.id.injury) as Button
        buttonIncrement = findViewById<View>(R.id.vial) as Button

        count = 10

        buttonRed.setOnClickListener {
            if (count >= 0) {
                count--
                textCount.text = count.toString()
            }
        }

        buttonIncrement.setOnClickListener {
            if (count <= 10)
                count += 3
            textCount.text = count.toString()
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.i("MyTag", "onSaveInstanceState")

        outState.putInt(COUNT_KEY, count)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.i("MyTag", "onRestoreInstanceState")

        count = savedInstanceState.getInt(COUNT_KEY)
    }
}

ViewModel 实现部分未被翻译。

英文:

Here is the full code, You didn't add many details but this is how the sample project looks, it enables to save count and then set it to TextView:

MainActivity.kt:

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    companion object {
        const val COUNT_KEY = &quot;COUNT_KEY&quot; // const key to save/read value from bundle
    }
    
    private var count = 0 // count value with setter. It will be easier, You can change this value and don&#39;t have to think about setting TextView.text
        set(value) {
            field = value
            txtCount.text = value.toString()
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.i(&quot;MyTag&quot;, &quot;onCreate&quot;)

        txtCount.text = count.toString()

        butIncrement.setOnClickListener {
            count++
        }

    }

    override fun onSaveInstanceState(outState: Bundle) { // Here You have to save count value
        super.onSaveInstanceState(outState)
        Log.i(&quot;MyTag&quot;, &quot;onSaveInstanceState&quot;)
        
        outState.putInt(COUNT_KEY, count)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) { // Here You have to restore count value
        super.onRestoreInstanceState(savedInstanceState)
        Log.i(&quot;MyTag&quot;, &quot;onRestoreInstanceState&quot;)

        count = savedInstanceState.getInt(COUNT_KEY)
    }
}

activity_main.xml:

&lt;androidx.appcompat.widget.LinearLayoutCompat xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/butIncrement&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Increment&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtCount&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:gravity=&quot;center&quot;
        android:textSize=&quot;40sp&quot; /&gt;

&lt;/androidx.appcompat.widget.LinearLayoutCompat&gt;

<hr>

You edited the question and added more details, here is the full Kotlin code, I tested it and it seems to work.

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    lateinit var textCount: TextView
    lateinit var buttonred: Button
    lateinit var buttoning: Button

    companion object {
        const val COUNT_KEY = &quot;COUNT_KEY&quot;
    }

    private var count = 0
        set(value) {
            field = value
            textCount.text = value.toString()
        }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.i(&quot;MyTag&quot;, &quot;onCreate&quot;)


        textCount = findViewById&lt;View&gt;(R.id.textView) as TextView
        buttonred = findViewById&lt;View&gt;(R.id.injury) as Button
        buttoning = findViewById&lt;View&gt;(R.id.vial) as Button

        count = 10

        buttonred.setOnClickListener {
            if (count &gt;= 0) {
                count--
                textCount.text = count.toString()
            }
        }

        buttoning.setOnClickListener {
            if (count &lt;= 10)
                count += 3
            textCount.text = count.toString()
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.i(&quot;MyTag&quot;, &quot;onSaveInstanceState&quot;)

        outState.putInt(COUNT_KEY, count)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.i(&quot;MyTag&quot;, &quot;onRestoreInstanceState&quot;)

        count = savedInstanceState.getInt(COUNT_KEY)
    }
}

<hr>

ViewModel implementation:

To Your Gradle (module.app) file add:

dependencies {
    def lifecycle_version = &quot;2.2.0&quot;

    // ViewModel
    implementation &quot;androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version&quot;
    // LiveData
    implementation &quot;androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version&quot;
}

MainActivityViewModel class:

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel


internal class MainActivityViewModel : ViewModel() {

    private val _count: MutableLiveData&lt;Int&gt; = MutableLiveData()
    val count: LiveData&lt;Int&gt;
        get() = _count

    init {
        _count.value = START_VALUE
    }

    fun increment() {
        _count.value = _count.value!! + 1
    }

    fun decrement() {
        _count.value = _count.value!! - 1

    }

    companion object {
        private const val START_VALUE = 10
    }
}

MainActivity class:

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider

class MainActivity : AppCompatActivity()
{
    private lateinit var viewModel: MainActivityViewModel

    lateinit var textCount: TextView
    lateinit var butIncrement: Button
    lateinit var butDecrement: Button

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

        textCount = findViewById&lt;View&gt;(R.id.textView) as TextView
        butIncrement = findViewById&lt;View&gt;(R.id.injury) as Button
        butDecrement = findViewById&lt;View&gt;(R.id.vial) as Button

        viewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)

        viewModel.count.observe(this, {
            textCount.text = it.toString()
        })

        butIncrement.setOnClickListener {
            viewModel.increment()
        }

        butDecrement.setOnClickListener {
            viewModel.decrement()
        }
    }
}

答案2

得分: 1

class MainActivity : AppCompatActivity() {
   private lateinit var resultTv: TextView
    private var count = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val buttonIncrease = findViewById<Button>(R.id.button_increase)
        val buttonDecrease = findViewById<Button>(R.id.button_decrease)
        resultTv = findViewById(R.id.result)
        buttonIncrease.setOnClickListener { view ->
            updateValue(view.id)
            resultTv.text = "" + count
        }
        buttonDecrease.setOnClickListener { view ->
            updateValue(view.id)
            resultTv.text = "" + count
        }
    }

    private fun updateValue(id: Int) {
        if (id == R.id.button_increase) {
            if (count < 10) count++
        } else {
            if (count > 0) count--
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("count", count)
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        savedInstanceState?.let {
           count = it.get("count") as Int
            resultTv.text = "" + count
        }
    }
}
英文:

You need to override onSaveInstanceState and onRestoreInstanceState example follows

class MainActivity : AppCompatActivity() {
private lateinit var resultTv: TextView
private var count = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val buttonIncrease = findViewById&lt;Button&gt;(R.id.button_increase)
    val buttonDecrease = findViewById&lt;Button&gt;(R.id.button_decrease)
    resultTv = findViewById(R.id.result)
    buttonIncrease.setOnClickListener { view -&gt;
        updateValue(view.id)
        resultTv.text = &quot;&quot; + count
    }
    buttonDecrease.setOnClickListener { view -&gt;
        updateValue(view.id)
        resultTv.text = &quot;&quot; + count
    }
}

private fun updateValue(id: Int) {
    if (id == R.id.button_increase) {
        if (count &lt; 10) count++
    } else {
        if (count &gt; 0) count--
    }
}

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    outState.putInt(&quot;count&quot;, count)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
    super.onRestoreInstanceState(savedInstanceState)
    savedInstanceState?.let {
       count = it.get(&quot;count&quot;) as Int
        resultTv.text = &quot;&quot; + count
    }
}

}

huangapple
  • 本文由 发表于 2020年9月2日 16:05:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63701251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定