Java 如何更改带有字符的按钮文本?
我想用输入文本视图的字符串字符更改按钮文本。我尝试了“settext”,但遇到了错误Java 如何更改带有字符的按钮文本?,java,android,button,character,Java,Android,Button,Character,我想用输入文本视图的字符串字符更改按钮文本。我尝试了“settext”,但遇到了错误 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); a=(Button)findViewById(R.id.button1); tv1=(TextV
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=(Button)findViewById(R.id.button1);
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
String word=tv2.getText().toString();
a.setText(word.charAt(3));
正如您自己所说,它需要一个
字符串。但是,charAt()
返回的是字符,而不是字符串
要解决此问题,请更改a.setText(word.charAt(3))
toa.setText(String.valueOf(word.charAt(3))代码>
这将把charAt()
输出的char
转换为字符串
,从而使其与正在更改的文本兼容
我希望这有帮助 当需要字符串时,它可能会抱怨它是一个字符。只需将word.charAt(3)
放在括号中,并向字符串添加强制转换:a.setText(string.valueOf(word.charAt(3))代码>“unfourtanetley,应用程序已停止”这可能是settext的问题,或者是字符问题。我尝试了相同的代码,用一个有效的字符串更改按钮文本。但这不是。。setText采用CharSequence
。请参阅public final void setText(CharSequence text)
非常感谢各位,用NitroNbg的解决方案解决它。