我从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”但它没有帮助
答案 0 :(得分:5)
定义一个返回格式化为每行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;
}
答案 1 :(得分:2)
我编辑了ramaral给出的答案,他使用了错误的转义序列
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;
}
答案 2 :(得分:0)
将以下2个属性添加到TextView定义中:
android:singleLine="false"
android:maxems="10"