简体   繁体   English

Android / Java:使用字符串索引的char设置textview

[英]Android/Java: Set textview with char from index of string

I want the user to put in characters into an edit text and upon a keyUp() would change a textview to match a certain char from a string. 我希望用户将字符放入编辑文本中,并且keyUp()会更改textview以匹配字符串中的某个char。

If a user put in 4 characters in the edittext string, then the index of 3 of the string would be set for the textview. 如果用户在edittext字符串中输入4个字符,则将为textview设置3个字符串的索引。

The problem is that the program crashes when checking the 0th index of the string. 问题是程序在检查字符串的第0个索引时崩溃。 This log command gives me the proper output, 这个log命令给了我正确的输出,

            String thedefaultstring = "Hello Android";
            //e_question is the edit text
            int stringsum = e_question.getText().toString().length();
            System.out.println(thedefaultstring.charAt(stringsum));

But when I set the textview with the char, it crashes. 但是当我使用char设置textview时,它会崩溃。

// t_debug3.setText(thedefaultstring.charAt(0)); //crashes

Try: 尝试:

setText(new String(thedefaultstring.charAt(stringSum));

setText takes a String as an argument. setTextString作为参数。 Build a new String of only one char to trick it. 构建一个只有一个char的新String来欺骗它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM