gpt4 book ai didi

java - setTextColor() - 逻辑错误

转载 作者:行者123 更新时间:2023-11-30 00:10:17 25 4
gpt4 key购买 nike

你好,我的简单 Android 应用程序有问题,它无法更改另一个 Activity 中的文本颜色,即 displayActivity.java 这是我的代码示例。

问题是如果文本都相等它将变成绿色成功但它确实变成了红色error

主 Activity .java

public class MainActivity extends AppCompatActivity {
public static final String xy = "ict402.germio.intent";
public static final String xz = "ict402.germio.intent";
@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());


if (strx.compareToIgnoreCase(stry) == 0)
{
// this line WILL print

Intent i = new Intent(this, displayActivity.class);

String t = ("Case Ignored \n VALUES ARE THE SAME CONGRATS!").toString();
i.putExtra(xy,t);
startActivity(i);


} else {
Intent i = new Intent(this, displayActivity.class);

String y = ("Case Ignored \n VALUES ARE NOT THE SAME SORRY!").toString();
i.putExtra(xz,y);
startActivity(i);

}


}
}

显示 Activity .java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);



Intent i = getIntent();
String message = i.getStringExtra(MainActivity.xy);
TextView t = findViewById(R.id.x);
t.setTextColor(getResources().getColor(R.color.success));
t.setText(message);



Intent o = getIntent();
String msg = o.getStringExtra(MainActivity.xz);

TextView q = findViewById(R.id.x);
q.setTextColor(getResources().getColor(R.color.error));
q.setText(msg);





}
}

最佳答案

错误的地方太多了。这是一个替代品:

public void send(View view) {
String editTextAContents = findViewById(R.id.a).getText().toString();
String editTextBContents = findViewById(R.id.b).getText().toString();

Intent intent = new Intent(this, DisplayActivity.class);

if (editTextAContents.equalsIgnoreCase(editTextBContents)) {
intent.putExtra("message", "Case Ignored \n VALUES ARE THE SAME CONGRATS");
intent.putExtra("error", false);
} else {
intent.putExtra("ict402.germio.intent", "Case Igored \n VALUES ARE NOT THE SAME SORRY!");
intent.putExtra("error", true);
}

startActivity(intent);
}

在 DisplayActivity 中:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);

Intent intent = getIntent();

String message = intent.getStringExtra("message");
boolean hasError = intent.getBooleanExtra("error", false);

TextView textView = findViewById(R.id.x);

textView.setText(message);

if (hasError) {
textView.setTextColor(ContextCompat.getColor(this, R.color.error));
} else {
textView.setTextColor(ContextCompat.getColor(this, R.color.success));
}
}

当您填充Intent 的额外内容时,它们必须具有不同的名称。

当你声明变量时,更详细而不是将它们命名为 x、y、z、a、b、c,这样它们更具可读性。

关于java - setTextColor() - 逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48248751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com