3 回答

TA貢獻1797條經驗 獲得超6個贊
做EditText這樣的事情
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="1" />
它只會從用戶那里獲取單個字符。現在你可以像這樣在 String 中轉換:
示例代碼:
Edittext et = (Edittext) findViewById(R.id.et1); // This will take Edittext of You're layout
String value = et.getText().toString(); // Get User input String
Character char = value.charAt(0); // Character
根據你的問題。
刪除最大長度。
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
代碼 :
Edittext et = (Edittext) findViewById(R.id.et1); // This will take Edittext of You're layout
String value = et.getText().toString(); // Get User input String
for(int i = 0; i < value.length(); i++){
Log.d("Char", value.charAt(i));
}

TA貢獻1829條經驗 獲得超13個贊
就像@mumpitz 提到的,text.charAt(0)
如果你只需要一個字符,你可以使用?;蛘?,如果您需要整個字符串,您可以使用text.toCharArray()
它來為您提供一個字符數組。

TA貢獻1816條經驗 獲得超4個贊
String
您可以從using中獲取一個字符str.getCharAt(index)
。這index
是您想要的字符串的字符索引。
此外,您不能將 a 更改String
為 char,但可以將 the 更改String
為 a array
of char。
String str = textView.getText().toString(); char[] chr = str.toCharArray();
此外,您可以使用該字符的索引從數組中獲取任何字符。
char c = chr[i]; // i is the index such as 0,1,2,3
添加回答
舉報