当前位置: 首页 > 知识库问答 >
问题:

类型不匹配:推断类型为字符串,但应为正则表达式

庄文栋
2023-03-14

所以,我已经将Android studio中的一段代码从Java转换为Kotlin,现在我得到了这些错误。

类型不匹配:推断的类型为字符串,但应为正则表达式任务:应用中的app:compiledBugkotlin已完成


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 ]*")
    }
}

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

错误为:类型不匹配:推断类型为字符串,但应为Regex任务:应用程序中的app:compiledBugKotlin已完成

共有1个答案

焦博实
2023-03-14

文件

字符串。匹配需要一个正则表达式对象作为参数。使用字符串。toRegex创建方法:

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

编辑,例如:

fun hasSymbol(value: String): Boolean {
    return !value.matches("[A-Za-z0-9 ]*".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())
}
 类似资料: