如何将单个字符分配给TextView [英] How to assign a single char to a TextView
本文介绍了如何将单个字符分配给TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为Android Java中的TextView分配一个字符.
使用字符串有效:
I'm trying to assign a single char to a TextView in Android Java.
Using a string works:
textView.setText("X");
但是使用char会在运行时中止:
But using a char aborts at runtime:
textView.setText('X');
和:
char Key5 = 'X';
textView.setText(Key5);
使用字符也会在运行时中止:
Using a Character also aborts at runtime:
Character Key5 = 'X';
textView.setText(Key5);
将字符类型转换为字符串确实有用:
Typecasting the Character to a string does work:
Character Key5 = 'X';
textView.setText(Key5.toString());
如何为TextView分配纯char变量?
How do I assign a plain char variable to a TextView?
推荐答案
您可以使用String.valueOf(char)方法将字符转换"为字符串:
You can "convert" a character into a String with the method String.valueOf(char):
char key = 'X';
textView.setText(String.valueOf(key));
这篇关于如何将单个字符分配给TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文