Java Android中使用显式意图的文本比较
输出总是Java Android中使用显式意图的文本比较,java,android,string-comparison,Java,Android,String Comparison,输出总是而不是女孩。如何比较amruta字符串 请告诉我这个声明是否正确 我使用了显式意图,希望比较两个字符串 即使我在editText字段中输入“amruta”,我也不会得到一个女孩 package com.satish.explicitintents; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import an
而不是女孩
。如何比较amruta字符串
请告诉我这个声明是否正确
我使用了显式意图,希望比较两个字符串
即使我在editText字段中输入“amruta”,我也不会得到一个女孩
package com.satish.explicitintents;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et = (EditText) findViewById(R.id.editText1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent(Main.this, Second.class);
intent.putExtra("thetext", et.getText().toString());
startActivity(intent);
}
});
}
}
-
使用equals()
方法代替下面的=
运算符
if (getIntent().getExtras().getString("thetext").equals( "amruta")) {
tv.setText("girl");
} else {
tv.setText("not a girl");
}
您还可以使用equalsIgnoreCase()
方法
String
的实例是对象,因此不能使用=
运算符比较两个对象。=
运算符用于与基本类型数据进行比较。要比较两个字符串
,必须使用equals()
或equalsIgnoreCase()
方法。使用equals()
方法,而不是下面的=
运算符
if (getIntent().getExtras().getString("thetext").equals( "amruta")) {
tv.setText("girl");
} else {
tv.setText("not a girl");
}
您还可以使用equalsIgnoreCase()
方法
String
的实例是对象,因此不能使用=
运算符比较两个对象。=
运算符用于与基本类型数据进行比较。要比较两个字符串
,必须使用equals()
或equalsIgnoreCase()
方法。在java编程中使用=
操作符来比较两个字符串是编程的一个坏迹象。您需要使用equals()
或equalsIgnoreCase()
方法。所以用这个:
if (getIntent().getExtras().getString("thetext").equalsIgnoreCase(amruta))
{
tv.setText("girl");
}
else {
tv.setText("not a girl");
}
在java编程中使用
==
操作符来比较两个字符串是编程的一个坏迹象。您需要使用equals()
或equalsIgnoreCase()
方法。所以用这个:
if (getIntent().getExtras().getString("thetext").equalsIgnoreCase(amruta))
{
tv.setText("girl");
}
else {
tv.setText("not a girl");
}
应使用
equals()
方法比较字符串。请搜索,此问题已被询问至少100次:)使用equals进行字符串比较应使用equals()
方法比较字符串。请搜索,此问题已被询问至少100次:)请使用等于进行字符串比较