长按 Material 设计按钮允许粘贴到按钮标签
Long Press Material Design Button Allows Pasting into Button Label
背景
我有一个标准的 Material 设计按钮,如下所示
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/medium_margin"
android:layout_marginRight="@dimen/medium_margin"
android:textStyle="bold"
tools:text="I am a Button"
style="@style/PrimaryColoredButton" />
应用于它的样式如下
<style name="PrimaryColoredButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:textColor">@color/white</item>
<item name="android:capitalize">sentences</item>
<item name="android:background">@drawable/primary_button_background</item>
</style>
问题
当按钮被长按时,系统粘贴按钮出现,允许用户将内容粘贴到按钮的标签中。
然后粘贴后:
从 XML 的角度来看,我看不出有什么方法可以阻止它,但感觉这根本不可能。
有趣的是,只有当长按不是在文本上而是在背景本身上时才会发生。
这种行为似乎并非 AppCompatButton
所独有。它源于 Button
是 TextView
的子类这一事实,而在您的案例中实际触发该行为的是自定义样式中的 capitalize
属性。当此 设置为 none
以外的任何值时,它会导致 TextView
的内部 Editor
变为活动状态并响应长按。
事实上,似乎任何导致 Button
具有默认输入类型以外的输入类型的属性设置都会导致这种情况。这些属性包括 - 但不限于 - capitalize
、digits
、autoText
,显然还有 none
以外的任何 inputType
。 textIsSelectable
属性也会造成一点破坏,因为 Button
将像不可编辑的 EditText
一样工作。将出现光标和选择手柄,以及适当的编辑 CAB,但没有输入法,Button
变得不可点击。
这些显然不是您通常会在 Button
上设置的属性,但这对我来说绝对像是一个错误,因为 Button
可能不应该以任何方式允许自己变得可编辑。事实上,在某些状态下,粘贴文本,然后移动焦点或单击会导致 Editor
崩溃。如果这是意外行为(粘贴,而不是崩溃),据我所知,它被错误地实施了一段时间。
要解决这个问题,您应该从样式中删除 capitalize
属性,并在 Button
上设置文本时自行处理。或者,如果您不需要响应 Button
上的长按,您可以将样式中的 longClickable
属性设置为 false
。
不幸的是,如果与上述任何其他有问题的属性结合使用,简单地将(已弃用的)editable
属性设置为 false
似乎并不能解决问题。
背景
我有一个标准的 Material 设计按钮,如下所示
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.AppCompatButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/medium_margin"
android:layout_marginRight="@dimen/medium_margin"
android:textStyle="bold"
tools:text="I am a Button"
style="@style/PrimaryColoredButton" />
应用于它的样式如下
<style name="PrimaryColoredButton" parent="Widget.AppCompat.Button.Colored">
<item name="android:textColor">@color/white</item>
<item name="android:capitalize">sentences</item>
<item name="android:background">@drawable/primary_button_background</item>
</style>
问题
当按钮被长按时,系统粘贴按钮出现,允许用户将内容粘贴到按钮的标签中。
然后粘贴后:
从 XML 的角度来看,我看不出有什么方法可以阻止它,但感觉这根本不可能。
有趣的是,只有当长按不是在文本上而是在背景本身上时才会发生。
这种行为似乎并非 AppCompatButton
所独有。它源于 Button
是 TextView
的子类这一事实,而在您的案例中实际触发该行为的是自定义样式中的 capitalize
属性。当此 设置为 以外的任何值时,它会导致 none
TextView
的内部 Editor
变为活动状态并响应长按。
事实上,似乎任何导致 Button
具有默认输入类型以外的输入类型的属性设置都会导致这种情况。这些属性包括 - 但不限于 - capitalize
、digits
、autoText
,显然还有 none
以外的任何 inputType
。 textIsSelectable
属性也会造成一点破坏,因为 Button
将像不可编辑的 EditText
一样工作。将出现光标和选择手柄,以及适当的编辑 CAB,但没有输入法,Button
变得不可点击。
这些显然不是您通常会在 Button
上设置的属性,但这对我来说绝对像是一个错误,因为 Button
可能不应该以任何方式允许自己变得可编辑。事实上,在某些状态下,粘贴文本,然后移动焦点或单击会导致 Editor
崩溃。如果这是意外行为(粘贴,而不是崩溃),据我所知,它被错误地实施了一段时间。
要解决这个问题,您应该从样式中删除 capitalize
属性,并在 Button
上设置文本时自行处理。或者,如果您不需要响应 Button
上的长按,您可以将样式中的 longClickable
属性设置为 false
。
不幸的是,如果与上述任何其他有问题的属性结合使用,简单地将(已弃用的)editable
属性设置为 false
似乎并不能解决问题。