这就是我正在做的事情
第一个 Activity
public void onClick(View v) {
Intent intent = new Intent(TeamsActivity.this,PakistaniPlayers.class).putExtra("StringName","hello");
startActivity(intent);
}
第二项 Activity
if(getIntent().getStringExtra("StringName")=="hello")
{
TextView t=(TextView) findViewById(R.id.textView1);
t.setText(getIntent().getStringExtra("StringName"));
}
现在 if 语句中的代码不会运行,但如果我单独运行 if 语句中的语句,则会显示 hello。
最佳答案
正如 @anil 已经说过的,你不能在 java 中比较这样的字符串。
String 类的三个主要函数可以比较两个字符串:
- 等于
- 等于忽略大小写
- 匹配
String test1 = "Hello";
String test2 = "hello";
test1.equalsIgnoreCase(test2); //isn't case-sensitive, so true.
test1.equals(test2); //is case-sensitive, so false.
test1.matches("[hH]ello"); //takes regexp, and will, in this case, be true.
test2.matches("[hH]ello"); //takes regexp, and will, in this case, be true.
一般提示:如果您不关心空格精确匹配,例如:“Hello”也应该匹配“Hello”,我建议您始终test1.trim()
您的字符串,特别是如果它是解析的内容。
关于java - 在 if 语句中使用 getIntent 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27590716/