【发布时间】:2019-01-05 21:02:33
【问题描述】:
我正在使用kotlin 创建一个安卓应用程序。我已经达到了我拥有 EULA(最终用户许可协议)checkbox 的程度,我需要使复选框的部分文本可点击以显示条款和条件。
XML 文件
<CheckBox
android:id="@+id/accept_terms_and_conditions"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="I have read and accept the Terms & Conditions"
android:textColor="#000000"
android:textSize="16sp"/>
我想让Terms & Conditions 部分可点击。我已经尝试过下面的代码。这最初是 java 代码找到 here 并由 Android Studio 自动转换为 kotlin 但我没有太多运气,因为应用程序在 SignUpActivity 启动时崩溃。
Kotlin 文件
class SignUpActivity : AppCompatActivity() {
private var mAuthTask: UserSignUpTask? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sign_up)
val checkBox = findViewById<TextView>(R.id.accept_terms_and_conditions)
val text = getText(R.string.terms_and_conditions)
val ss = SpannableString(text)
val clickableSpan1 = object : ClickableSpan() {
override fun onClick(widget: View) {
Toast.makeText(this@SignUpActivity, "One", Toast.LENGTH_SHORT).show()
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.color = Color.BLUE
ds.isUnderlineText = false
}
}
ss.setSpan(clickableSpan1, 28, 46, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
checkBox.text = ss
checkBox.movementMethod = LinkMovementMethod.getInstance()
}
}
那么有没有办法直接从XML 文件中执行此操作?如果不是,我该如何在Kotlin 中做到这一点?
注意:我正在开发应用的 API 是 21。
【问题讨论】:
标签: android android-studio checkbox kotlin