按钮文本没有改变
问题描述:
我试图通过变量(语言)来改变文本,但即使语言变量的值是“阿尔巴尼亚”,按钮的文本也没有改变!按钮文本没有改变
P.S变量值从另一个活动传递。
String language="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_level_activity_layout);
btnBegginer=(Button)findViewById(R.id.btnBegginer);
btnMedium=(Button)findViewById(R.id.btnMedium);
btnHard=(Button)findViewById(R.id.btnHard);
Intent objIntent=getIntent();
language=objIntent.getStringExtra("language");
//Toast.makeText(getApplicationContext(),language,Toast.LENGTH_LONG).show();
if (language=="albanian")
{
btnBegginer.setText("FILLESTAR");
btnMedium.setText("MESATARE");
btnHard.setText("VESHTIRE");
}
else
{
btnBegginer.setText("BEGGINER");
btnMedium.setText("MEDIUM");
btnHard.setText("HARD");
}
答
永远不要使用==
比较字符串使用.equals()
if (language.equals("albanian"))
{
btnBegginer.setText("FILLESTAR");
btnMedium.setText("MESATARE");
btnHard.setText("VESHTIRE");
}
else
{
btnBegginer.setText("BEGGINER");
btnMedium.setText("MEDIUM");
btnHard.setText("HARD");
}
+0
非常感谢。我在Java方面相对较新 – KosovoDev
答
当比较Java中的字符串时,使用equals()
方法。例如:stringValue.equals("Test")
返回true/false。
+0
非常感谢。我在Java相对较新 – KosovoDev
你不应该硬编码字符串,尤其是如果它受到定位。您应该将字符串放入'string.xml'文件中,并根据系统设置提供替代文件。查看[this](https://developer.android.com/guide/topics/resources/localization.html)了解更多详情 – Sunshinator