【问题标题】:Type mismatch: inferred type is String but Regex was expected类型不匹配:推断类型为字符串,但应使用正则表达式
【发布时间】:2021-09-26 11:10:47
【问题描述】:

所以,我已将 Android Studio 中的代码从 Java 转换为 Kotlin,现在我遇到了这些错误。

类型不匹配:推断类型为字符串,但预期为正则表达式 任务:app:compileDebugKotlin in app Finished


class MainActivity : AppCompatActivity() {
    lateinit var tvUsername: EditText
    lateinit var tvPassword: EditText
    lateinit var btnRegister: Button
    lateinit var img1: ImageView
   lateinit var img2: ImageView
   lateinit var img3: ImageView
   lateinit var img4: ImageView
   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tvUsername = findViewById(R.id.et_userName)
        this.tvPassword = findViewById(R.id.et_password)
        this.btnRegister = findViewById(R.id.btnRegister)
        img1 = findViewById(R.id.img1)
        img2 = findViewById(R.id.img2)
        img3 = findViewById(R.id.img3)
        img4 = findViewById(R.id.img4)
        tvPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (hasLength(s)) {
                    img1.setImageResource(R.drawable.checked)
                } else {
                    img1.setImageResource(R.drawable.unchecked)
                }
                if (hasSymbol(s)) {
                    img4.setImageResource(R.drawable.checked)
                } else {
                    img4.setImageResource(R.drawable.unchecked)
                }
                if (hasDigit(s)) {
                    img3.setImageResource(R.drawable.checked)
                } else {
                    img3.setImageResource(R.drawable.unchecked)
                }
                if (hasUppercase(s)) {
                    img2.setImageResource(R.drawable.checked)
                } else {
                    img2.setImageResource(R.drawable.unchecked)
                }
                if (hasLength(s) && hasUppercase(s) && hasDigit(s) && hasSymbol(s)) {
                    btnRegister.setVisibility(View.VISIBLE)
                } else {
                    btnRegister.setVisibility(View.GONE)
                }
            }

            override fun afterTextChanged(s: Editable) {}
        })
    }

    fun hasLength(value: CharSequence): Boolean {
        return value.toString().length >= 8
    }

    fun hasDigit(value: CharSequence): Boolean {
        return value.toString() matches("(.*\\d.*)")
    }

    fun hasUppercase(value: CharSequence): Boolean {
        val s = value.toString()
        return s != s.isLowerCase()
    }

    fun hasSymbol(value: CharSequence): Boolean {
        val s = value.toString()
        return !s.matches("[A-Za-z0-9 ]*")
    }
}

以上是完整的代码。我在 hasUppercase() 和 hasSymbol 函数中遇到问题。 请帮忙。

错误是 :Type mismatch: inferred type is String but Regex is expected Task :app:compileDebugKotlin in app Finished

【问题讨论】:

    标签: android-studio kotlin


    【解决方案1】:

    Docs

    String.matches 需要 Regex 对象作为参数。使用String.toRegex 方法创建一个:

    s.matches("[A-Za-z0-9 ]*".toRegex())
    

    编辑,例如:

    fun hasSymbol(value: String): Boolean {
        return !value.matches("[A-Za-z0-9 ]*".toRegex())
    }
    

    再次编辑

    您没有使用 toRegex 函数作为模式,s != s.isLowerCase() 真的没有意义。以下应该可以工作。

    fun hasLength(value: CharSequence): Boolean {
        return value.length >= 8
    }
    
    fun hasDigit(value: CharSequence): Boolean {
        return value matches "(.*\\d.*)".toRegex()
    }
    
    fun hasUppercase(value: CharSequence): Boolean {
        return value matches "[A-Z]*".toRegex()
    }
    
    fun hasSymbol(value: CharSequence): Boolean {
        return !(value matches "[A-Za-z0-9 ]*".toRegex())
    }
    

    【讨论】:

    • 其实我是 Kotlin 的新手,所以请您输入 hasSymbol() 所需的代码。这对我来说真的很有用。
    • @praritdhawan 查看我的编辑
    • 感谢您的帮助,但在包含正则表达式部分 ibb.co/RDjB7W0 后仍然出现错误,这是错误图像的链接
    • @praritdhawan 再次查看我的编辑
    • @praritdhawan 顺便说一句,假设您正在尝试检查 CharSequence 是否包含一个或多个“符号”,您的 hasSymbol 函数实际上不起作用。
    猜你喜欢
    • 2019-03-26
    • 2021-09-15
    • 2020-04-24
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多