我有一个按钮(像)是从字符串资源写入的,点击我想切换到不喜欢&反之。比较字符串存储在Android的字符串资源?
如何比较按钮包含像或不同于字符串资源?
我string.xml包含
<string name="like">Like</string>
<string name="unlike>unlike</string>
我有一个按钮(像)是从字符串资源写入的,点击我想切换到不喜欢&反之。比较字符串存储在Android的字符串资源?
如何比较按钮包含像或不同于字符串资源?
我string.xml包含
<string name="like">Like</string>
<string name="unlike>unlike</string>
您可以使用getString方法
if(btn1.getText().toString().compareTo(getResources().getString(R.string.app_name)) == 0)
{
// do some code here
}
说按钮ID是btn1&在字符串资源字符串名称是像&不像然后 – napster 2012-07-27 07:01:40
下面的代码使用
Button button = (Button)findViewById(R.id.btn1);
if(button.getText().toString().equals(getString(R.string.like)))
{
button.setText(getString(R.string.unlike));
}
else
{
button.setText(getString(R.string.like));
}
尝试为:
String text = getResources().getString(R.string.texttest);
Button button = (Button)this.findViewById(R.id.btntest);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(button.getText().toString().equals(text))
{
// do some code here
}
}
});
您可以用这种方式检索标签(按钮上的文本)。
Button button=(Button) findViewById(R.id.button1);
Log.d("Text",""+button.getText());
你可以这样做::
String value=buttonname.getText().toString
//this Will Give the Text On the Button
现在比较喜欢这种方式的价值,并更改名称的按钮::
if(value.equals("Like"))
{
buttonname.setText("UnLike");
}
else
{
buttonname.setText("Like");
}
中试试这个..
bt.setText(getString(R.string.txt1));
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myStr=bt.getText().toString();
if((myStr.equals(getString(R.string.txt1)))){
bt.setText(getString(R.string.txt2));
}else if((myStr.equals(getString(R.string.txt2)))){
bt.setText(getString(R.string.txt1));
}
}
});
在string.xml中
<string name="txt1">11111111111</string>
<string name="txt2">2222222222222222</string>
看到我的答案并接受正确的答案。 – 2012-07-27 07:13:52