【发布时间】:2019-10-25 21:29:31
【问题描述】:
我的MainActivity.java 文件中有此代码,但是当在编辑文本中输入任何值并按下按钮时,相关字符串不会打印。我怎样才能使它在按下按钮时根据在编辑文本槽中输入的字符串显示某个文本?
public void ShowAge(View view){
EditText entered = (EditText)findViewById(R.id.typehere);
TextView value = (TextView)findViewById(R.id.answer);
if(entered.getText().toString() == "Dog"){
value.setText(R.string.dog);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
value.setTypeface(typeFace);}
else if(entered.getText().toString() == "Cat"){
value.setText(R.string.cat);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
value.setTypeface(typeFace);}
else if(entered.getText().toString() == "Meal"){
value.setText(R.string.meal);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
value.setTypeface(typeFace);}
else if(entered.getText().toString() == "Chocolate"){
value.setText(R.string.chocolate);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
value.setTypeface(typeFace);}
else if(entered.getText().toString() == "Froyo"){
value.setText(R.string.froyo);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
value.setTypeface(typeFace);}
}
【问题讨论】:
-
尝试使用
equalsignorecase而不是==来比较字符串