我的应用程序中有一个Edittext。 我在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中时,文本会以其他颜色而不是黑色的形式出现。
如何使文本的颜色标准化为黑色,无论我从哪种颜色复制它。
更新:
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) {
}
});
layout
文件。 - Satan Pandeyaandroid:textAppearance="?android:attr/textAppearanceLarge"
是一个系统值,我猜它并没有完全定义颜色、样式等。我认为你需要用自己的样式来尝试一下。这只是一个尝试,我不知道它是否能解决问题。这里有一个简单的例子,可以看看Sebastian Roth的答案,像那样尝试一下。https://dev59.com/NW435IYBdhLWcg3wkBBe - Opiatefuchs