如何将单个字符分配给TextView

Hum*_*000 3 java android

我正在尝试将单个字符分配给Android Java中的TextView.
使用字符串有效:

textView.setText("X");
Run Code Online (Sandbox Code Playgroud)

但是在运行时使用char中止:

textView.setText('X');  
Run Code Online (Sandbox Code Playgroud)

和:

char Key5 = 'X';  
textView.setText(Key5);
Run Code Online (Sandbox Code Playgroud)

使用Character也会在运行时中止:

Character Key5 = 'X';  
textView.setText(Key5);
Run Code Online (Sandbox Code Playgroud)

将字符类型转换为字符串确实有效:

Character Key5 = 'X';  
textView.setText(Key5.toString());
Run Code Online (Sandbox Code Playgroud)

如何将纯字符变量分配给TextView?

小智 9

您可以使用String.valueOf(char)方法将字符"转换"为String:

char key = 'X';
textView.setText(String.valueOf(key));
Run Code Online (Sandbox Code Playgroud)