我想使用textview显示多行文本。每行只显示20个字符,怎么可能。
我已设置android:maxLines="5"
android:maxLength="20"
,但它无效。
这里只显示20个字符,不显示其他字符。 任何建议。
答案 0 :(得分:1)
在构建函数中没有使用它可以执行此操作。 android:maxLength也是用于设置文本视图中允许的最大字符数的属性,因此pl删除它。要存档您想要的内容,您可以按照user2450263给出的link进行操作。它是每行10个字符,你想要的是20.所以下面是从链接修改代码。
public String getTenCharPerLineString(String text){
String tenCharPerLineString = "";
while (text.length() > 20) {
String buffer = text.substring(0, 20);
tenCharPerLineString = tenCharPerLineString + buffer + "\n";
text = text.substring(20);
}
tenCharPerLineString = tenCharPerLineString + text.substring(0);
return tenCharPerLineString;
}
答案 1 :(得分:0)
如果您非常想实现这一目标,那就是
use 3 EditText boxes(number of lines you want )
let say et1, et2, et3.
并添加addTextChangedListener(new TextWatcher(){});
像这样
et1.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(et1.getText().toString().length()==20){
et2.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
et2.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(et2.getText().toString().length()==20){
et3.requestFocus();
}
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
然后最后String result = et1.getText.toString()+ et2.getText.toString()+ et3.getText.toString()
答案 2 :(得分:0)
尝试使用此方法来运行时而不是从xml
public void doEllipsize(final TextView tv, final int maxLine) {
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = tv.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if (maxLine <= 0) {
int lineEndIndex = tv.getLayout().getLineEnd(0);
String text = tv.getText().subSequence(0, lineEndIndex - 3) + "...";
tv.setText(text);
} else if (tv.getLineCount() >= maxLine) {
int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
String text = tv.getText().subSequence(0, lineEndIndex - 3) + "...";
tv.setText(text);
}
}
});
}
如何使用?
textView = (TextView)findViewById(R.id.textView);
doEllipsize(tv,5);
答案 3 :(得分:-1)
删除android:maxLength =“20”
见:
<TextView
android:id="@+id/textview"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:maxLines="5"
android:lines="5"
android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."></TextView>