布局xml文件中有一个TextView,如下所示:
<TextView
android:id="@+id/viewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/string_id" />我的字符串指定如下:
<string name="string_id">text</string>有没有可能让它在没有java代码的情况下显示"Text“而不是"text”
(也不改变字符串本身)
发布于 2013-09-04 22:29:59
不是的。但是您可以创建一个简单的CustomView扩展TextView,它覆盖setText并将第一个字母大写成大写字母,就像Ahmad这样说的那样,并在您的setText布局中使用它。
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class CapitalizedTextView extends TextView {
public CapitalizedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setText(CharSequence text, BufferType type) {
if (text.length() > 0) {
text = String.valueOf(text.charAt(0)).toUpperCase() + text.subSequence(1, text.length());
}
super.setText(text, type);
}
}发布于 2014-10-07 13:02:49
我用Hyrum Hammon的答案设法把所有的单词都大写了。
public class CapitalizedTextView extends TextView {
public CapitalizedTextView( Context context, AttributeSet attrs ) {
super( context, attrs );
}
@Override
public void setText( CharSequence c, BufferType type ) {
/* Capitalize All Words */
try {
c = String.valueOf( c.charAt( 0 ) ).toUpperCase() + c.subSequence( 1, c.length() ).toString().toLowerCase();
for ( int i = 0; i < c.length(); i++ ) {
if ( String.valueOf( c.charAt( i ) ).contains( " " ) ) {
c = c.subSequence( 0, i + 1 ) + String.valueOf( c.charAt( i + 1 ) ).toUpperCase() + c.subSequence( i + 2, c.length() ).toString().toLowerCase();
}
}
} catch ( Exception e ) {
// String did not have more than + 2 characters after space.
}
super.setText( c, type );
}
}发布于 2019-03-28 10:07:37
作为Kotlin扩张函数
fun String.capitalizeFirstCharacter(): String {
return substring(0, 1).toUpperCase() + substring(1)
}
textview.text = title.capitalizeFirstCharacter()https://stackoverflow.com/questions/18624273
复制相似问题