您好,我的简单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);





    }
}

最佳答案

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

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

07-28 12:46