【问题标题】:Clickable checkBox text in android with Kotlin使用 Kotlin 在 android 中可点击的复选框文本
【发布时间】: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 &amp; Conditions"
    android:textColor="#000000"
    android:textSize="16sp"/>

我想让Terms &amp; 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


    【解决方案1】:

    在@Karan Mer 的帮助和找到here 的一些代码的帮助下,问题得到了解决,所以我将在这里介绍整体解决方案。

    XML 文件

    <LinearLayout
            android:id="@+id/checkbox_Layout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <CheckBox
                android:id="@+id/accept_terms_and_conditions"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
    
            <TextView
                android:id="@+id/Terms_and_condition_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="0dp"
                android:layout_marginBottom="0dp"
                android:text="I have read and accept the Terms & Conditions"
                android:textColor="#000000"
                android:textSize="16sp"/>
        </LinearLayout>
    

    Kotlin 文件

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sign_up)
    
    
    
        val textView = findViewById<TextView>(R.id.Terms_and_condition_text)
    
        val text = getText(R.string.terms_and_conditions)
    
        val ss = SpannableString(text)
    
        val clickableSpan1 = object : ClickableSpan() {
            override fun onClick(widget: View) {
                //here you can set it to do whatever you want when clicked
            }
    
    
            //This is in order to change the default appereance of the link
            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = Color.BLUE
                ds.isUnderlineText = false
            }
        }
    
        //here you set the starting and ending char of the link in the string
        ss.setSpan(clickableSpan1, 28, 45, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    
        textView.text = ss
        textView.movementMethod = LinkMovementMethod.getInstance()
    }
    

    【讨论】:

    • 这是在点击 textview 时获得背景颜色,为什么会这样?
    【解决方案2】:

    您可以这样做,将水平 LinearLayout 与不带文本的 CheckBox 和要显示为复选框文本的 TextView 一起使用。现在您可以单独处理对复选框和文本视图的点击。

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
            <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
    
            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="I agree to terms and conditions"/>
        </LinearLayout>
    

    结果如下所示。

    【讨论】:

    • 那我怎样才能只设置Terms and Conditions部分可点击?
    • 将点击监听设置为仅文本视图。
    • 这会将孔 textveiw 设置为可点击,我只想要 Terms &amp; Conditions 部分,因此我可以将其设为蓝色并看起来像您在大多数 EULA 复选框中看到的链接
    • 我正在寻找类似this 但在kotlin 中的内容。
    • 您可以将 textview 的宽度设置为 wrapcontent,因此它实际上类似于单击您的文本,除非您提供足够大的填充。关于更改文本颜色会很容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多