Java 在按钮的setText()方法中使用串联值的/one正确方法是什么?
我尝试使用一个连接的值来设置按钮的文本,因为Java 在按钮的setText()方法中使用串联值的/one正确方法是什么?,java,android,string,concatenation,Java,Android,String,Concatenation,我尝试使用一个连接的值来设置按钮的文本,因为intxp在运行时计算。 唯一有效的方法是发出警告,也许是糟糕的做法 你有没有更好的解决办法 我试过以下几种方法: 第一次进近: public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable { private int xp = 2000; public int getXp(){
intxp代码>在运行时计算。
唯一有效的方法是发出警告,也许是糟糕的做法
你有没有更好的解决办法
我试过以下几种方法:
第一次进近:
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + String.valueOf(getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + (getXp()));}
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText("XP: " + (getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(getResources().getIdentifier("praefixXP", "string", getPackageName()) + getXp());}
}
结果:
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + String.valueOf(getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + (getXp()));}
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText("XP: " + (getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(getResources().getIdentifier("praefixXP", "string", getPackageName()) + getXp());}
}
编译,但为按钮指定一个int/numeric值
第二种方法:
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + String.valueOf(getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + (getXp()));}
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText("XP: " + (getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(getResources().getIdentifier("praefixXP", "string", getPackageName()) + getXp());}
}
结果:
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + String.valueOf(getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + (getXp()));}
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText("XP: " + (getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(getResources().getIdentifier("praefixXP", "string", getPackageName()) + getXp());}
}
抛出资源$NotFoundException
第三种方法:
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + String.valueOf(getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + (getXp()));}
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText("XP: " + (getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(getResources().getIdentifier("praefixXP", "string", getPackageName()) + getXp());}
}
结果:
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + String.valueOf(getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + (getXp()));}
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText("XP: " + (getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(getResources().getIdentifier("praefixXP", "string", getPackageName()) + getXp());}
}
工作正常,但需要注释:
@SuppressLint({"SetTextI18n"})
第四种方法:
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + String.valueOf(getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + (getXp()));}
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText("XP: " + (getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(getResources().getIdentifier("praefixXP", "string", getPackageName()) + getXp());}
}
结果:
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + String.valueOf(getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(R.string.praefixXP + (getXp()));}
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText("XP: " + (getXp())); }
}
public class selectionActivity extends AppCompatActivity implements View.OnClickListener, Serializable {
private int xp = 2000;
public int getXp(){
return xp;
}
@Override
public void onResume() {
super.onResume();
Button currentXp1 = findViewById(R.id.currentXp1);
currentXp1.setText(getResources().getIdentifier("praefixXP", "string", getPackageName()) + getXp());}
}
抛出资源$NotFoundException
strings.xml:
XP:
您需要使用
currentXp1.setText((getResources().getString(R.string.praefixXP)+getXp()));
要从字符串资源文件中获取字符串值,我们需要使用getResources().getString(string_ID)
,在本例中,我只是将getXp()的结果与字符串的值连接起来
使用currentXp1.setText(getResources().getString(R.string.praefixXP),getXp())可以随意询问是否有不清楚的地方;引发错误消息:错误:找不到适用于setText(String,int)方法TextView的方法。setText(CharSequence,BufferType)不适用(参数不匹配;int无法转换为BufferType)方法TextView。setText(int,BufferType)不适用(参数不匹配;String无法转换为int)getXp()的返回类型是什么尝试使用currentXp1.setText((getResources().getString(R.string.praefixXP)+getXp());太好了,现在开始工作了!非常感谢你!很高兴帮助你。