在使用kotlin合成

时间:2018-05-09 12:38:32

标签: android android-fragments kotlin synthetic

当值存在时,TextView不会显示文本。在onViewCreated内部片段中调用此操作。在相同的情况下,OnClickListener也无法正常工作。 我搜索过类似的问题,但没有找到完全相同的情况。我错过了什么?谢谢你的帮助。

import kotlinx.android.synthetic.main.fragment_login.*
---

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                      savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_login, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val isLogin = activity?.let { PrefHelper.getBoolean(it, PrefHelper.IS_LOGIN, false) }
    if (isLogin != null && isLogin) {
        onSignedIn()
    } else {
        onSignedOut()
    }
}

private fun onSignedIn() {
    form_login.visibility = View.GONE
    user_profile.visibility = View.VISIBLE

    var sEmail = ""
    var sFullName = ""
    var sPhone = ""
    if (activity != null) {
        PrefHelper.putBoolean(activity!!, PrefHelper.IS_LOGIN, true)
        sEmail = PrefHelper.getString(activity!!, PrefHelper.USER_EMAIL, "")
        sFullName = PrefHelper.getString(activity!!, PrefHelper.USER_FULL_NAME, "")
        val sPhonePrefix = PrefHelper.getString(activity!!, PrefHelper.USER_PHONE_PREFIX, "")
        val sPhoneSuffix = PrefHelper.getString(activity!!, PrefHelper.USER_PHONE_SUFFIX, "")
        sPhone = "$sPhonePrefix $sPhoneSuffix"
    }

    user_email.text = sEmail
    user_full_name.text = sFullName
    user_phone.text = sPhone

    logout.setOnClickListener { onSignedOut() }
}

the UI

breakpoint

0 个答案:

没有答案