长按材质设计按钮允许粘贴到按钮标签中 [英] Long Press Material Design Button Allows Pasting into Button Label
问题描述
背景
如下所示,我有一个标准的材料设计"按钮
I have a standard Material Design button, as per below
<?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>
问题
长按按钮时,将显示系统粘贴按钮,并允许用户将内容粘贴到按钮的标签中.
When the button is long pressed, the system paste button appears and allows the user to paste content into the Button's label.
粘贴后:
从XML的角度来看,我看不出有办法阻止这种情况的出现,但是感觉这根本不可能实现.
I cannot see a way to stop this from an XML perspective, but it feels like this shouldn't be possible at all.
有趣的是,仅当长按不是在文本上而是在背景本身上时,才会发生这种情况.
Interestingly it only happens when the long press is not on the text but the background itself.
推荐答案
看来,这种行为不是AppCompatButton
所独有的.它源于Button
是TextView
的子类的事实,在您的情况下实际触发该行为的是您自定义样式中的capitalize
属性.如果将此设置为以外的任何其他值,则会导致none
TextView
的内部Editor
变为活动状态并响应长按.
That behavior is not exclusive to AppCompatButton
s, it seems. It stems from the fact that Button
is a subclass of TextView
, and what's actually triggering that behavior in your case is the capitalize
attribute in your custom style. When this is set to anything other than , it causes none
TextView
's internal Editor
to become active and respond to long clicks.
实际上,似乎任何导致Button
具有除默认输入类型以外的输入类型的属性设置都将导致这种情况.这些属性包括-(但不限于)-capitalize
,digits
,autoText
,并且显然包括none
以外的任何inputType
. textIsSelectable
属性也会造成一些破坏,因为Button
的行为就像不可编辑的EditText
一样.光标和选择柄将与适当的编辑CAB一起出现,但没有输入方法,并且Button
不可单击.
In fact, it appears that any attribute setting that causes the Button
to have an input type other than the default will cause this. These attributes include - but are not limited to - capitalize
, digits
, autoText
, and, obviously, any inputType
other than none
. The textIsSelectable
attribute will cause a little havoc, too, in that the Button
will act like an un-editable EditText
. The cursor and selection handle(s) will appear, along with the editing CAB when appropriate, but no input method, and the Button
becomes un-clickable.
这些显然不是通常在Button
上设置的属性,但是对我来说这绝对是个错误,因为Button
可能不应该以任何方式使其自身变得可编辑.实际上,在某些状态下,粘贴文本,然后转移焦点或单击会导致Editor
崩溃.如果这是意外的行为(粘贴,而不是崩溃),那么据我所知,它已经被错误地实施了一段时间.
These are clearly not attributes you would normally be setting on a Button
, but this definitely seems like a bug to me, since Button
probably shouldn't, in any way, allow itself to become editable. Indeed, in certain states, pasting text, then shifting focus or clicking causes the Editor
to crash. If this is unintended behavior (the pasting, not the crashing), it's been wrongly implemented for some time, as far as I can tell.
要解决此问题,应从样式中删除capitalize
属性,并在Button
上设置文本时自行处理.另外,如果您不需要对Button
的长按做出响应,则可以将样式中的longClickable
属性设置为false
.
To remedy this, you should remove the capitalize
attribute from your style, and just handle that yourself when you set the text on the Button
. Alternatively, if you don't need to respond to long clicks on your Button
s, you could set the longClickable
attribute in your style to false
.
不幸的是,如果将(已弃用的)editable
属性设置为false
,似乎不能解决问题,如果将其与上述任何其他有问题的属性结合使用的话.
Unfortunately, simply setting the (deprecated) editable
attribute to false
doesn't seem to be a fix, if combined with any of the other above-mentioned, problematic attributes.
这篇关于长按材质设计按钮允许粘贴到按钮标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!