gpt4 book ai didi

java - 我不允许在 switch case [Android] 中传递资源文件中的字符串

转载 作者:太空狗 更新时间:2023-10-29 15:41:21 25 4
gpt4 key购买 nike

我有几个按钮,我有以下相同的点击监听器:

 private View.OnClickListener onclick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.home:
break;
case R.id.contact:
break;
case R.id.terms:
break;
case R.id.touch:
show(R.string.about_us); //Error here
break;


}
}
};

在每次点击按钮时,我都会显示相同的弹出窗口,只是文本不同。我在 strings.xml 文件中有文本

下面是我的对话函数:

 public void show(String message){
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.terms);

//dialog.setTitle("Terms & Conditions");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(message);
text.setTypeface(helv_light);
ImageButton dialogButton = (ImageButton) dialog.findViewById(R.id.button1);
// dialogButton.setTypeface(helv_light);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();
}

万一 R.id.touch: 我的 IDE 提示以下消息:

show(java.lang.string) 不能应用于 (int)。

如果我将 show(R.string.about_us); 替换为 show(""+R.string.about_us); 错误消失了,我该怎么办想念这里吗?

最佳答案

使用 getResources().getStringstrings.xml 获取字符串:

show(v.getContext().getResources().getString(R.string.about_us));

关于java - 我不允许在 switch case [Android] 中传递资源文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28565989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com