java - 在 if 语句中使用 getIntent 函数

标签 java android

这就是我正在做的事情

第一个 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/

相关文章:

android - 库模块中的数据绑定(bind)

java - 如何读取文本文件并使用它来填充对象数组的数据?

java - 使用 android studio 时 apk 在设备上持续崩溃

java - 应为 <标识符>。 java

java - fusedLocationProviderClient 不工作和 MapActivity android

Android TextView 以及如何确定可见字符数

android - 预期为BEGIN_OBJECT,但在路径Moshi Json数据异常处为BEGIN_ARRAY

java - com.example.RedisApplication 中方法存储库的参数 0 需要找不到类型的 bean

Android iBeacon接收单个指定信号

android - OpenGL ES 和线程结构