Android: textColor actually doesn't work

enter image description hereI have an Edittext in my application. I set the default color for black as follows in XML:

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

VIEW:

<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>

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

How can I standardize the color to be black, no matter what color I copied it.

UPDATE:

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) {
            }
        });
+4
source share
2 answers

Depending on the functionality they added, the behavior of the copy depends on the vendor and vendor.

: onFocusChangeListener editText, editText , textColor! . .

:
editText, , .

editText. GONE. editText android:imeOptions="actionNext".
"EditText field, set it input type to TYPE_NULL . Now, when the user presses button in the keyborad, your EditText` , textColor. , .

0

ok, . , , . :

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

TextWatcher 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:

 private ClipboardManager clipBoard;
 private boolean addedToClipboard = false;

:

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

                addedToClipboard = true;
            }
        });

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);

   }
 }

, . , .

, , , , EditText. , Google, editText, , .

0

Source: https://habr.com/ru/post/1663188/


All Articles