亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 textView 文本轉換為 char 變量?

如何將 textView 文本轉換為 char 變量?

慕桂英4014372 2023-03-31 14:27:25
所以我試圖將用戶輸入的 Edittext 文本轉換為 char 變量,但我似乎無法使其工作。另外,沒有找到任何有用的東西,所以決定在這里問一下。
查看完整描述

3 回答

?
FFIVE

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));

}


查看完整回答
反對 回復 2023-03-31
?
烙印99

TA貢獻1829條經驗 獲得超13個贊

就像@mumpitz 提到的,text.charAt(0)如果你只需要一個字符,你可以使用?;蛘?,如果您需要整個字符串,您可以使用text.toCharArray()它來為您提供一個字符數組。



查看完整回答
反對 回復 2023-03-31
?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

String您可以從using中獲取一個字符str.getCharAt(index)。這index是您想要的字符串的字符索引。
此外,您不能將 a 更改String為 char,但可以將 the 更改String為 a arrayof char。

String str = textView.getText().toString();
char[] chr = str.toCharArray();

此外,您可以使用該字符的索引從數組中獲取任何字符。

char c = chr[i]; // i is the index such as 0,1,2,3


查看完整回答
反對 回復 2023-03-31
  • 3 回答
  • 0 關注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號