gpt4 book ai didi

android:每行 TextView 限制为 10 个字符

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:16 26 4
gpt4 key购买 nike

我从 EditText 读取值并将其写入 TextView

editTitle1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
s = editTitle1.getText().toString();
textTitle1.setText(s);

}
});

我希望它在一行中 - 最多 10 个字符,所以如果所有 20 个字符 - 它在 TextView 中是两行,每行 10 个字符。我该怎么做?我尝试 android:maxLength="10"但它没有帮助

最佳答案

定义一个方法,返回一个格式化为每行 10 个字符的字符串:

public String getTenCharPerLineString(String text){

String tenCharPerLineString = "";
while (text.length() > 10) {

String buffer = text.substring(0, 10);
tenCharPerLineString = tenCharPerLineString + buffer + "/n";
text = text.substring(10);
}

tenCharPerLineString = tenCharPerLineString + text.substring(0);
return tenCharPerLineString;
}

关于android:每行 TextView 限制为 10 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21364923/

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