gpt4 book ai didi

java - Android 如何从 TextView 获取一个字符串到一个字符串

转载 作者:搜寻专家 更新时间:2023-11-01 08:01:42 25 4
gpt4 key购买 nike

我知道如何将整数值转化为字符串

String s = st.getText().toString(0;

但我的问题是我们将如何取一个字符...假设 textView txt1 的值 = 'A' 那么我们如何将值取为字符串 str1..

String str1 = txt1.getText().toString();

是否正确……?

最佳答案

TextView.getText() 返回 java.lang.CharSequence ,理想情况下,您应该能够应用 toString() 来获取 String 值。但是,然而,

来自 CharSequence java doc

This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence is therefore, in general, undefined. Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other. It is therefore inappropriate to use arbitrary CharSequence instances as elements in a set or as keys in a map.

CharSequence 是一个接口(interface)(尽管 toString() 显示为已声明),只要 CharSequence 实现者正确完成了他们的工作,它就应该可以工作。为避免任何意外,您可以尝试不同的方法

final StringBuilder sb = new StringBuilder(charSequence.length());
sb.append(charSequence);
return sb.toString();

关于java - Android 如何从 TextView 获取一个字符串到一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20511573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com