setTextColor() - 逻辑错误

时间:2018-01-14 10:42:21

标签: java android

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

问题是,如果文本都相同,它将变为绿色success 但它确实变成了红色error

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

        }


    }
}

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





    }
}

2 个答案:

答案 0 :(得分:1)

有太多错误。这是一个替代品:

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,以便它们更具可读性。

答案 1 :(得分:0)

您没有使用任何条件逻辑来确定要使用的颜色。

你需要做这样的事情:

TextView t = findViewById(R.id.x);
String successMessage = getIntent().getStringExtra(MainActivity.xy);
String errorMessage = getIntent().getStringExtra(MainActivity.xz);

if(successMessage != null){
    t.setTextColor(getResources().getColor(R.color.success));
    t.setText(successMessage);
}else if(errorMessage != null){
    t.setTextColor(getResources().getColor(R.color.error));
    t.setText(errorMessage);
}

更好的方法是通过意图发送布尔值来帮助您确定要设置的颜色。这是一个例子:

Intent i = new Intent(this, displayActivity.class);
if (strx.equalsIgnoreCase(stry)){
     i.putExtra("message","Case Ignored \n VALUES ARE THE SAME CONGRATS!");
     i.putExtra("success", true);
} else {
     i.putExtra("message","Case Ignored \n VALUES ARE NOT THE SAME SORRY!");
     i.putExtra("success", false);
}
startActivity(i);

在其他活动中,请执行以下操作:

TextView t = findViewById(R.id.x);
Intent i = getIntent();
String message = getIntent().getStringExtra("message");
boolean success = getIntent().getBooleanExtra("success");
t.setText(message);
t.setTextColor(getResources().getColor(success ? R.color.success : R.color.error));