好的我正在尝试创建一个只接受任何文本的1个单词的应用程序。我使用edittext让用户输入文本和按钮,让他们给你一个包含错误或正确单词的祝酒词
问题是当我使用if语句我不能让它工作时可以请一些人在这里找出问题是我的代码
public class IfActivity extends Activity {
Button GO;
EditText TEXT;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GO = (Button) findViewById(R.id.go);
TEXT = (EditText) findViewById(R.id.Textin);
GO.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if ("hello".equals(TEXT)) {
Toast andEggs = Toast.makeText(IfActivity.this,
"PASSWORD IS CORRECT", Toast.LENGTH_SHORT);
andEggs.show();
} else {
Toast andEggs = Toast.makeText(IfActivity.this,
"PASSWORD IS INCORRECT", Toast.LENGTH_SHORT);
andEggs.show();
}
}
});
}
}
答案 0 :(得分:3)
TEXT
不是字符串。它是EditText的引用变量。
if (TEXT.getText().toString().equals("hello"))
{
}
答案 1 :(得分:1)
使用此:
if("hello".equals((findViewById(R.id.Textin)).getText().toString())) { // show toast}
答案 2 :(得分:1)
使用此:
public void onClick(View arg0) {
// TODO Auto-generated method stub
String tempTxt = ((EditText) findViewById(R.id.Textin)).getText().toString();
if ("hello".equals(tempTxt )) {
Toast andEggs = Toast.makeText(IfActivity.this,"PASSWORD IS CORRECT",Toast.LENGTH_SHORT);
andEggs.show();
}
else{
Toast andEggs = Toast.makeText(IfActivity.this,"PASSWORD IS INCORRECT",Toast.LENGTH_SHORT);
andEggs.show();
}
}