将条款和条件的提交保存为 Android studio 中的首选项?

问题描述 投票:0回答:1

我正在尝试为用户第一次打开应用程序时弹出的应用程序实现“条款和条件”屏幕。当他们接受条款和条件后,他们会继续使用该应用程序。如果他们拒绝,应用程序将自动关闭。我向 ScrollView 添加了一个 TextView 和 2 个按钮。当用户一直滚动到底部时,这 2 个按钮将被启用,他们可以选择下一步要做什么。

package com.example.termsandconditionsview

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewTreeObserver
import android.widget.Button
import android.widget.ScrollView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(), View.OnTouchListener, ViewTreeObserver.OnScrollChangedListener {

    private var svMain: ScrollView? = null
    private lateinit var buttonAccept: Button
    private lateinit var buttonDecline: Button

    private lateinit var sharedPref: SharedPreferences

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

        svMain = findViewById(R.id.scrollView2)
        svMain?.viewTreeObserver?.addOnScrollChangedListener(this)
        buttonAccept = findViewById(R.id.button_accept)
        buttonDecline = findViewById(R.id.button_decline)

        sharedPref = getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE)

        val termsAccepted = sharedPref.getBoolean("terms_accepted", false)
        if (termsAccepted) {
            setContentView(R.layout.activity_main)
        } else {
            setContentView(R.layout.terms_and_conditions_view)
        }
    }

    override fun onScrollChanged() {
        svMain?.let { scrollView ->
            val view = scrollView.getChildAt(scrollView.childCount - 1) as View
            val bottomDetector = view.bottom - (scrollView.height + scrollView.scrollY)

            if (bottomDetector <= 0) {
                buttonAccept.isEnabled = true
                buttonDecline.isEnabled = true
            } else {
                buttonAccept.isEnabled = false
                buttonDecline.isEnabled = false
            }
            buttonAccept.setOnClickListener {
                // Update the termsAccepted value to true in SharedPreferences
                val editor = sharedPref.edit()
                editor.putBoolean("terms_accepted", true)
                editor.apply()

                val textAcceptTac = getString(R.string.txt_accept_tac)
                val duration = Toast.LENGTH_LONG
                val toastAccept = Toast.makeText(this, textAcceptTac, duration)
                toastAccept.show()

                setContentView(R.layout.activity_main)
                // Proceed with the app as the user has accepted the terms&conditions
            }
            buttonDecline.setOnClickListener{
                val textDeclineTac = getString(R.string.txt_decline_tac)
                val duration = Toast.LENGTH_LONG
                val toastDecline = Toast.makeText(this, textDeclineTac, duration)
                toastDecline.show()
                // Close app when user clicks the "Decline" button.
                finish()
            }
        }
    }

    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
        TODO("Not yet implemented")
    }
}

现在的问题是我的偏好部分有问题。在我添加之前,禁用/启用按钮以及 On.Click 事件侦听器都有效。添加首选项代码后,不再起作用。

我不知道问题是什么。有人可以帮我解决这个问题吗?谢谢! :)

android-studio sharedpreferences
1个回答
0
投票

您设置了 setContentView() 两次。这意味着您在该行之前初始化的所有视图都将为空。

separate method to initialize the layout views and listeners

class MainActivity : AppCompatActivity(), View.OnTouchListener, ViewTreeObserver.OnScrollChangedListener {

    private var svMain: ScrollView? = null
    private lateinit var buttonAccept: Button
    private lateinit var buttonDecline: Button
    private lateinit var sharedPref: SharedPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        sharedPref = getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE)
        val termsAccepted = sharedPref.getBoolean("terms_accepted", false)

        if (termsAccepted) {
            setContentView(R.layout.activity_main)
        } else {
            setContentView(R.layout.terms_and_conditions_view)
            initializeViewsAndListeners()
        }
    }

    private fun initializeViewsAndListeners() {
        svMain = findViewById(R.id.scrollView2)
        svMain?.viewTreeObserver?.addOnScrollChangedListener(this)
        buttonAccept = findViewById(R.id.button_accept)
        buttonDecline = findViewById(R.id.button_decline)

        buttonAccept.setOnClickListener {
            // Update the termsAccepted value to true in SharedPreferences
            val editor = sharedPref.edit()
            editor.putBoolean("terms_accepted", true)
            editor.apply()

            val textAcceptTac = getString(R.string.txt_accept_tac)
            Toast.makeText(this, textAcceptTac, Toast.LENGTH_LONG).show()

            setContentView(R.layout.activity_main)
        }

        buttonDecline.setOnClickListener {
            val textDeclineTac = getString(R.string.txt_decline_tac)
            Toast.makeText(this, textDeclineTac, Toast.LENGTH_LONG).show()
            finish()
        }
    }

    override fun onScrollChanged() {
        svMain?.let { scrollView ->
            val view = scrollView.getChildAt(scrollView.childCount - 1) as View
            val bottomDetector = view.bottom - (scrollView.height + scrollView.scrollY)

            if (bottomDetector <= 0) {
                buttonAccept.isEnabled = true
                buttonDecline.isEnabled = true
            } else {
                buttonAccept.isEnabled = false
                buttonDecline.isEnabled = false
            }
        }
    }

    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
        TODO("Not yet implemented")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.