因此,我一直在查看getText()方法,并了解到它返回一个CharSequence。所以你不能就这么做
TextView myTextView = (TextView) findViewById(R.id.my_text_view);
String myString = myTextView.getText();相反,必须通过以下操作将返回的CharSequence转换为字符串:
TextView myTextView = (TextView) findViewById(R.id.my_text_view);
String myString = myTextView.getText().toString();我的问题来了:你就不能这样做吗?:
TextView myTextView = (TextView) findViewById(R.id.my_text_view);
String myString = (String) myTextView.getText();我在我的代码中尝试过这一点,它运行得非常好,但是每个人似乎都在使用第一种方法..我的做法有什么问题吗?或者这只是一种不同的方式,如果是的话,这两种方式的好处是什么?
谢谢你提前给出的答案:)
发布于 2017-01-24 17:17:26
我的做法有什么问题吗?
如果返回的ClassCastException不是String,它将与String崩溃。例如,如果使用Html.fromHtml()或其他创建SpannedString的方法,并在TextView中使用该方法,getText()将不会返回String。
https://stackoverflow.com/questions/41834332
复制相似问题