android:textColor 实际上不起作用 [英] android:textColor not actually working

查看:62
本文介绍了android:textColor 实际上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个 Edittext.我在 XML 中以下列方式将其默认颜色设置为黑色:

I have an Edittext in my application. I have set it's default color to black in the following manner in the XML:

android:textColor="@android:color/black"

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/layout"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            tools:context="scientificcalculatorapp.scientificcalculator.ScientificCalculator"
            android:weightSum="1"
            android:orientation="vertical"
            android:layout_alignParentBottom="true">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@android:color/transparent"
                android:focusable="true"
                android:focusableInTouchMode="true">
            </LinearLayout>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/Output"
                android:enabled="true"
                android:focusable="true"
                android:longClickable="true"
                android:inputType="text"
                android:textIsSelectable="true"
                android:cursorVisible="true"
                android:textColor="@android:color/black"
                android:bufferType="spannable"
                android:background="@android:color/darker_gray"
                android:allowUndo="true" />
</LinearLayout>

这在我从键盘输入时有效,但是当我从不同的应用程序中复制不同颜色的内容,然后将其粘贴到此 EditText 中时,文本会以另一种颜色而不是黑色粘贴.

This works when I get input from my keypad but when I copy something in a different color from a different application and then paste it in this EditText, the text gets pasted in this other color and not black.

如何将颜色标准化为黑色,而不管我复制的是什么颜色.

How can I standardize the color to be black regardless of whatever color I copied it in.

更新:

output.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                String yourCopiedString=output.getText().toString();
                int length = yourCopiedString.length();
                Spannable spannable= new SpannableString(yourCopiedString);
                //set color
                //set size
                spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0,length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable.setSpan(new RelativeSizeSpan(5.0f), 0,length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                output.setText(spannable);
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
            }
        });

推荐答案

好的,来试试看.我不知道它是否解决了问题,但需要显示代码.首先创建一个样式:

ok, here is a try. I don´t know if it solves the problem, but need to show code. First create a style:

<style name="NormalText" parent="@android:style/TextAppearance">
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textSize">15sp</item>
 </style>

然后将 TextWatcher 添加到您的 EditText

then add a TextWatcher to your EditText

output.addTextChangedListener(new TextWatcher(){

       @Override
   public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

       @Override
   public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

       @Override
   public void afterTextChanged(Editable s) {


           }
}

创建一个全局布尔值和 ClipboardManager:

create a global boolean value and ClipboardManager:

 private ClipboardManager clipBoard;
 private boolean addedToClipboard = false;

初始化并向管理器添加一个监听器:

initialize and add a listener to the Manager:

 clipBoard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        clipBoard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {

                addedToClipboard = true;
            }
        });

然后在 AfterTextChanged 中执行以下操作:

Then do the following in AfterTextChanged:

    @Override
public void afterTextChanged(Editable s) {
     Log.d("TAPPJABB", "AFTER TEXT CHANGED:" + s);
     if (addedToClipboard == true) {
      String yourCopiedString = output.getText().toString();
      int length = yourCopiedString.length();
      Spannable spannable = new SpannableString(yourCopiedString);//set color
      spannable.setSpan(new ForegroundColorSpan(Color.BLACK), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      spannable.setSpan(new RelativeSizeSpan(5.0f), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      addedToClipboard = false;
      output.setText(spannable);

   }
 }

重要的是要遵循此处的顺序以避免无限循环.布尔值防止正常输入被格式化,在本例中它仅适用于剪贴板粘贴.

It´s important to follow the order here to avoid infinite loop. The boolean value prevents a normal input from beeing formatted, in this example it works only for clipboard paste.

但是就像你提到的,如果你想多次粘贴它,它就不起作用,通常没有人在一个 EditText 上做.这里的问题是,我们被谷歌留在了这里,没有粘贴侦听器或其他用于 editText 的解决方案,至少我找不到任何东西.

But like you mentioned, it works not if you want to paste it multiple times, what usually nobody do on one EditText. The problem here is, that we are left alone here from Google, there is no paste-listener or another solution for the editText, at least I couldn´t find anything.

这篇关于android:textColor 实际上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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