[英]Alternating editText text color onButtonClick
I am currently trying to create a highlighter function for my app where on a button being clicked by the user the program checks to see if the editText TextColor is black, default, or Yellow, highlighted, then switches between the two.我目前正在尝试为我的应用程序创建荧光笔 function,在用户单击按钮时,程序会检查 editText TextColor 是黑色、默认还是黄色、突出显示,然后在两者之间切换。
I am using an onClickListener if Statement:我正在使用 onClickListener if 语句:
hB0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ed1.getTextColors().equals(Color.BLACK)){
ed1.setTextColor(Color.YELLOW);
} else {
ed1.setTextColor(Color.BLACK);
}
}
});
I have set the textColor of the EditText to Color.Black in the XML file and I have looked for similar problems with little to show for it.我已在 XML 文件中将 EditText 的 textColor 设置为 Color.Black,并且我一直在寻找类似的问题,但几乎没有什么可显示的。 Thank you for your time.
感谢您的时间。
Edit: The problem is when I press the button the text stays the same colour and doesn't switch between the two.编辑:问题是当我按下按钮时,文本保持相同的颜色并且不会在两者之间切换。
Edit2: To take it further, when I comment out the if statement and make the button simply change the text colour to Color.YELLOW it works perfectly. Edit2:更进一步,当我注释掉 if 语句并使按钮简单地将文本颜色更改为 Color.YELLOW 时,它可以完美运行。 I just want to be able to switch the colour BACK to black.
我只想能够将颜色切换回黑色。
Edit 3: The XML Code for the highlighter button and the textView I want to change the text color in.编辑 3:荧光笔按钮的 XML 代码和 textView 我想更改文本颜色。
<Button
android:id="@+id/highLighter0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:minWidth="4dp"
android:minHeight="10dp"
android:text="H"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/slot0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@+id/slot0"
app:layout_constraintTop_toTopOf="@+id/slot0" />
<EditText
android:id="@+id/slot0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="center"
android:hint="6 AM"
android:inputType="textCapSentences|textNoSuggestions|textMultiLine"
android:lines="4"
android:maxLines="4"
android:minLines="1"
android:rotationX="0"
android:textColor="@color/black"
app:layout_constraintEnd_toStartOf="@+id/highLighter0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoppingListButton"
tools:layout_conversion_absoluteHeight="45dp"
tools:layout_conversion_absoluteWidth="370dp" />
EditText @id=slot0 is ed1 in the java code. EditText @id=slot0 是 java 代码中的 ed1。
You need to change your code with below code:您需要使用以下代码更改您的代码:
if (ed1.getCurrentTextColor() == Color.BLACK) {
ed1.setTextColor(Color.YELLOW);
} else {
ed1.setTextColor(Color.BLACK);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.