在Android中怎么使用button按钮把页面的值存储下来

setting.kt文件

package cn.ykuav.payload

import android.annotation.SuppressLint
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import cn.ykuav.payload.service.CacheNetHost
import cn.ykuav.payload.service.EmitterHost
import cn.ykuav.payload.service.LightHost
import cn.ykuav.payload.service.ShoutHost
import cn.ykuav.payload.service.YA3Host
class SettingActivity : AppCompatActivity() {
    private lateinit var preferences: SharedPreferences  //定义SharedPreferences对象

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)


        val save = findViewById<Button>(R.id.save)
        var Shout  = findViewById<EditText>(R.id.ShoutHostIP)
        val Light = findViewById<EditText>(R.id.LightHostIP)
        val CacheNet = findViewById<EditText>(R.id.CacheNetHostIP)
        val Emitter = findViewById<EditText>(R.id.EmitterHostIP)
        val YA3 = findViewById<EditText>(R.id.YA3HostIP)

        //        获取了 SharedPreferences 对象
        preferences = getSharedPreferences("myPreferences", MODE_PRIVATE)
        // 从 SharedPreferences 中读取上次存储的值,并显示在 Shout  中
        val value = preferences.getString("ShoutHost", "")                         
        val value2 = preferences.getString("LightHost", "") 
        val value3 = preferences.getString("CacheNetHost", "") 
        val value4 = preferences.getString("EmitterHost", "")
        val value5 = preferences.getString("YA3Host", "")
        if (value=="") { //当Shout的值为空值是设为默认值
            Shout.setText(ShoutHost)
        } else {//否则就设置为你修改的值
            Shout.setText(value)
        }

        if (value2=="") {
            Light.setText(LightHost)
        } else {
            Light.setText(value2)
        }

        if (value3=="") {
            CacheNet.setText(CacheNetHost)
        } else {
            CacheNet.setText(value3)
        }
        if (value4=="") {

            Emitter.setText(EmitterHost)
        } else {
            Emitter.setText(value4)
        }
        if (value5=="") {

            YA3.setText(YA3Host)
        } else {
            YA3.setText(value5)
        }

        save.setOnClickListener {
            val text = Shout.text.toString()
            val text1 = Light.text.toString()
            val text2 = CacheNet.text.toString()
            val text3 = Emitter.text.toString()
            val text4 = YA3.text.toString()
            // 将修改后的值存储到 SharedPreferences 中
            val editer =preferences.edit()
            editer.putString("ShoutHost", text)
            editer.putString("LightHost", text1)
            editer.putString("CacheNetHost", text2)
            editer.putString("EmitterHost", text3)
            editer.putString("YA3Host", text4)
            editer.apply()
            finish();  //直接关闭当前页面
        }
    }
}

setting.xml文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/setView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/setting"
                android:textColor="@color/white"
                android:textSize="24sp"
                android:textAlignment="center"
                android:layout_centerInParent="true"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="20dp"
                />



        </LinearLayout>

    </ScrollView>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            android:paddingRight="8dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:textAlignment="center"
                android:text="@string/ShoutHost_name" />

            <EditText
                android:id="@+id/ShoutHostIP"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="false"
                android:background="@drawable/boder"
                />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            android:paddingRight="8dp">

            <TextView
                android:layout_width="59dp"
                android:layout_height="20dp"
                android:text="@string/LightHost_name"
                android:textAlignment="center" />

            <EditText
                android:id="@+id/LightHostIP"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/boder"
                />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            android:paddingRight="8dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:textAlignment="center"
                android:text="@string/CacheNetHost_name" />

            <EditText
                android:id="@+id/CacheNetHostIP"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/boder"
                />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            android:paddingRight="8dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:textAlignment="center"
                android:text="@string/EmitterHost_name" />

            <EditText
                android:id="@+id/EmitterHostIP"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/boder"
                />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            android:paddingRight="8dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:textAlignment="center"
                android:text="@string/YA3Host_name" />

            <EditText
                android:id="@+id/YA3HostIP"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/boder"
                />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="vertical"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:gravity="right">

            <Button
                android:id="@+id/save"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:textAlignment="center"
                android:text="@string/save_name" />
        </LinearLayout>
    </LinearLayout>
    </ScrollView>

</LinearLayout>









以上就是我项目的两个文件

我参考了一下代码:

在 Android Kotlin 中,你可以使用SharedPreferences来存储页面的值,以便下次进入页面时显示修改后的值。以下是一个示例代码,演示如何使用 Button 按钮将页面的值存储下来,并在下次进入页面时显示修改后的值:

import android.app.Activity
import android.content.SharedPreferences
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText

class Main : Activity() {
    private lateinit var editText: EditText
    private lateinit var button: Button
    private lateinit var preferences: SharedPreferences

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

        editText = findViewById(R.id.editText)
        button = findViewById(R.id.button)

        preferences = getSharedPreferences("myPreferences", MODE_PRIVATE)

        // 从 SharedPreferences 中读取上次存储的值,并显示在 EditText 中
        val value = preferences.getString("value", "")
        editText.setText(value)

        button.setOnClickListener {
            val text = editText.text.toString()
            // 将修改后的值存储到 SharedPreferences 中
            preferences.edit().putString("value", text).apply()
        }
    }
}

在上述代码中,首先创建了一个名为 Main 的 Activity,并在布局文件中添加了一个 EditText 和一个 Button。然后,在onCreate()方法中获取了 SharedPreferences 对象,并从 SharedPreferences 中读取上次存储的值,将其显示在 EditText 中。
当点击 Button 按钮时,会获取 EditText 中的值,并将其存储到 SharedPreferences 中。下次进入页面时,会从 SharedPreferences 中读取存储的值,并显示在 EditText 中。
请注意,SharedPreferences 的数据存储在本地,因此只有在同一个设备上才能访问到存储的值。如果需要在不同设备上共享数据,可以考虑使用其他方式来存储和共享数据,例如网络存储或云存储。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值