长按材质设计按钮允许粘贴到按钮标签中 [英] Long Press Material Design Button Allows Pasting into Button Label

查看:94
本文介绍了长按材质设计按钮允许粘贴到按钮标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

如下所示,我有一个标准的材料设计"按钮

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所独有的.它源于ButtonTextView的子类的事实,在您的情况下实际触发该行为的是您自定义样式中的capitalize属性.如果将此设置为none 以外的任何其他值,则会导致TextView的内部Editor变为活动状态并响应长按.

That behavior is not exclusive to AppCompatButtons, 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 none, it causes TextView's internal Editor to become active and respond to long clicks.

实际上,似乎任何导致Button具有除默认输入类型以外的输入类型的属性设置都将导致这种情况.这些属性包括-(但不限于)-capitalizedigitsautoText,并且显然包括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 Buttons, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆