textInputについても言及していません。TextViewに静的テキスト(ユーザーが入力したデータへのデータベース呼び出しから入力された(大文字ではない可能性があります))がある場合、それらが大文字になっていることを確認するにはどうすればよいですか?
ありがとう!
textInputについても言及していません。TextViewに静的テキスト(ユーザーが入力したデータへのデータベース呼び出しから入力された(大文字ではない可能性があります))がある場合、それらが大文字になっていることを確認するにはどうすればよいですか?
ありがとう!
回答:
これは、AndroidやTextViewに固有のものではなく、標準のJava文字列操作で実現できるはずです。
何かのようなもの:
String upperString = myString.substring(0, 1).toUpperCase() + myString.substring(1).toLowerCase();
これを達成する方法はおそらく百万通りありますが。文字列のドキュメントを参照してください。
編集済み
私は追加しました.toLowerCase()
以下はTextViewには適用されませんが、EditTextでは機能します。それでも、setText()でロードされたテキストではなく、キーボードから入力されたテキストに適用されます。具体的には、キーボードのキャップをオンにし、ユーザーは自由にこれを上書きできます。
android:inputType="textCapSentences"
または
TV.sname.setInputType(InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
これにより、最初の文字が大文字になります。
または
compile 'org.apache.commons:commons-lang3:3.4' //in build.gradle module(app)
tv.setText(StringUtils.capitalize(myString.toLowerCase().trim()));
Kotlinの場合は、
textview.text = string.capitalize()
あなたは追加することができますApacheのCommonsのラング
のGradleなどにをcompile 'org.apache.commons:commons-lang3:3.4'
そして使用する WordUtils.capitalizeFully(name)
将来の訪問者のために、次に示すように、アプリWordUtilからインポートしてApache、アプリに多くの便利なメソッドを追加することもできます(最高のIMHO)capitalize。
私にとっては何も働いていません:
関数:
private String getCapsSentences(String tagName) {
String[] splits = tagName.toLowerCase().split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splits.length; i++) {
String eachWord = splits[i];
if (i > 0 && eachWord.length() > 0) {
sb.append(" ");
}
String cap = eachWord.substring(0, 1).toUpperCase()
+ eachWord.substring(1);
sb.append(cap);
}
return sb.toString();
}
結果:
I / P brain O / P脳
I / P Brain and Health O / P Brain And Health
I / P brain And healthからO / P Brain And Health
I / P brain's HealthからO / P Brain's Health
I / P brain's Health and legからO / P Brain's Health And Leg
これがお役に立てば幸いです。
受け入れられた答えは良いですが、AndroidのtextViewから値を取得するためにそれを使用している場合は、文字列が空かどうかを確認することをお勧めします。文字列が空の場合、例外がスローされます。
private String capitizeString(String name){
String captilizedString="";
if(!name.trim().equals("")){
captilizedString = name.substring(0,1).toUpperCase() + name.substring(1);
}
return captilizedString;
}
カスタムTextViewを作成して使用してください:
public class CustomTextView 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);
}
}
ここでは、いくつかのオプションがあるため、このトピックに関する詳細な記事を書きました。Androidで最初の文字列を大文字にする
Javaで文字列の最初の文字を大文字にする方法
public static String capitalizeString(String str) {
String retStr = str;
try { // We can face index out of bound exception if the string is null
retStr = str.substring(0, 1).toUpperCase() + str.substring(1);
}catch (Exception e){}
return retStr;
}
Kotlinで文字列の最初の文字を大文字にする方法
fun capitalizeString(str: String): String {
var retStr = str
try { // We can face index out of bound exception if the string is null
retStr = str.substring(0, 1).toUpperCase() + str.substring(1)
} catch (e: Exception) {
}
return retStr
}
XML属性の使用
または、この属性をTextViewまたはXMLのEditTextで設定できます
android:inputType="textCapSentences"