【问题标题】:Android: How to display a screen for the first time only in Kotlin?Android:如何仅在 Kotlin 中首次显示屏幕?
【发布时间】:2020-04-11 03:22:11
【问题描述】:

我的应用程序有一个初始屏幕(活动),要求用户输入他们的姓名,然后单击登录按钮。用户执行此操作后,我不希望应用程序再次显示此屏幕,而是应用程序应该启动另一个活动。我做了一些研究,发现您应该使用共享偏好,但我仍然感到困惑。以下是初始屏幕活动的代码:

class StudentNameInput : AppCompatActivity(), View.OnClickListener {

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

       btnStudentLogIn.setOnClickListener(this)
    }

    private fun validate(): Boolean {
        if (txt_student_name.text.toString().isEmpty()){
            txt_student_name.error = "Name cannot be empty"
            return false
        }

        return true
    }

    override fun onClick(v: View?){
        when(v?.id){
            R.id.btnStudentLogIn->{
                if(validate()){
                    Log.i(null, "setOnClickListener")
                    val intent = Intent(this, StudentInitialActivity::class.java)
                    startActivity(intent)
                }
            }
        }
    }

【问题讨论】:

    标签: android kotlin sharedpreferences


    【解决方案1】:

    您应该将用户名保存在共享首选项中,然后在开始活动时需要检查共享首选项是否保存了用户名,如果是,则开始下一个活动并完成当前活动,但如果没有,则留在当前的活动,如下所示:

    class StudentNameInput : AppCompatActivity(), View.OnClickListener {
    
        private lateinit var sharedPref: SharedPreferences
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
           sharedPref: SharedPreferences = getSharedPreferences("YOUR_PREF_NAME", Context.MODE_PRIVATE)
    
            if (wasUserNameSaved()) {
                val intent = Intent(this, StudentInitialActivity::class.java)
                startActivity(intent)
                finish()
                return
            }
            setContentView(R.layout.activity_student_name_input)
    
           btnStudentLogIn.setOnClickListener(this)
        }
    
        private fun validate(): Boolean {
            if (txt_student_name.text.toString().isEmpty()){
                txt_student_name.error = "Name cannot be empty"
                return false
            }
    
            return true
        }
    
        override fun onClick(v: View?){
            when(v?.id){
                R.id.btnStudentLogIn->{
                    if(validate()){
                        Log.i(null, "setOnClickListener")
     sharedPref.edit().putString("user_name", txt_student_name.text.toString()).apply()
                        val intent = Intent(this, StudentInitialActivity::class.java)
                        startActivity(intent)
                    }
                }
            }
        }
    
        private fun wasUserNameSaved(): Boolean {
       return sharedPref.getString("user_name", "").isNotEmpty()
        }
    

    抱歉,我用手机写的格式错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      相关资源
      最近更新 更多