Android应用setTextColor()逻辑错误:displayActivity无法正确切换文本颜色
Let's break down exactly why your success message is showing up in red instead of green, and fix it step by step:
What's Causing the Problem?
You've got two critical issues in your code:
Duplicate Intent Extra Keys
InMainActivity.java, you definedxyandxzwith the exact same string value:public static final String xy = "ict402.germio.intent"; public static final String xz = "ict402.germio.intent";This means you're using the same key to pass both success and error messages—so even if you intended to send different data, the Intent can only hold one value for that key, leading to confusion in the receiving activity.
Unconditional Overwriting in displayActivity
The biggest culprit is indisplayActivity.java: you're executing both success and error text/color setting code every time, regardless of which message was sent. Since the error code runs after the success code, it always overwrites the color and text to red, no matter what. To make it worse,tandqboth reference the sameTextView(R.id.x), so you're just modifying the same view twice.
Step-by-Step Fix
1. Fix the Intent Extra Keys in MainActivity
First, give your success and error messages unique keys so the receiving activity can tell them apart:
public class MainActivity extends AppCompatActivity { // Unique keys for each message type public static final String EXTRA_SUCCESS_MSG = "ict402.germio.intent.success"; public static final String EXTRA_ERROR_MSG = "ict402.germio.intent.error"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void send(View view){ EditText a = findViewById(R.id.a); EditText b = findViewById(R.id.b); String strx = a.getText().toString(); String stry = b.getText().toString(); Intent i = new Intent(this, displayActivity.class); if (strx.compareToIgnoreCase(stry) == 0) { i.putExtra(EXTRA_SUCCESS_MSG, "Case Ignored \n VALUES ARE THE SAME CONGRATS!"); } else { i.putExtra(EXTRA_ERROR_MSG, "Case Ignored \n VALUES ARE NOT THE SAME SORRY!"); } startActivity(i); } }
2. Rewrite displayActivity to Check Which Message Was Sent
Instead of running both code blocks, check which extra exists and set the color/text accordingly:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display); Intent intent = getIntent(); TextView resultText = findViewById(R.id.x); // Check if success message exists String successMsg = intent.getStringExtra(MainActivity.EXTRA_SUCCESS_MSG); if (successMsg != null) { resultText.setText(successMsg); resultText.setTextColor(getResources().getColor(R.color.success)); } // If not, use the error message else { String errorMsg = intent.getStringExtra(MainActivity.EXTRA_ERROR_MSG); resultText.setText(errorMsg); resultText.setTextColor(getResources().getColor(R.color.error)); } }
Alternative: Use a Boolean Flag for Simplicity
Another clean approach is to send a boolean along with the message to indicate success, instead of using two separate keys:
MainActivity.java (updated send method):
public void send(View view){ EditText a = findViewById(R.id.a); EditText b = findViewById(R.id.b); String strx = a.getText().toString(); String stry = b.getText().toString(); Intent i = new Intent(this, displayActivity.class); boolean isMatch = strx.compareToIgnoreCase(stry) == 0; // Send the message and success flag i.putExtra("EXTRA_MESSAGE", isMatch ? "Case Ignored \n VALUES ARE THE SAME CONGRATS!" : "Case Ignored \n VALUES ARE NOT THE SAME SORRY!"); i.putExtra("EXTRA_IS_SUCCESS", isMatch); startActivity(i); }
displayActivity.java:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display); Intent intent = getIntent(); TextView resultText = findViewById(R.id.x); String message = intent.getStringExtra("EXTRA_MESSAGE"); boolean isSuccess = intent.getBooleanExtra("EXTRA_IS_SUCCESS", false); resultText.setText(message); resultText.setTextColor(getResources().getColor(isSuccess ? R.color.success : R.color.error)); }
Either approach will fix your color issue by ensuring only the correct color and text are applied based on the input match.
内容的提问来源于stack exchange,提问作者Neil

