Как создать многострочный текст редактирования с представлением прокрутки в деятельности?
Я хочу edittext, который будет иметь 3 строки, и если содержание edittext превышает ограничение в 3 строки, то вертикальная полоса прокрутки должна быть видна пользователю.
7 ответов
Может работать на 100%
<EditText
android:inputType="textMultiLine"
android:lines="int number" <!-- Total Lines prior display -->
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbars="vertical"/>
Использование inputType="textMultiLine"
и установить android:lines="3"
лайк:
<EditText
android:inputType="textMultiLine"
android:lines="3" <!-- Total Lines prior display -->
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbars="vertical"
/>
Я думаю, что этот вопрос больше о добавлении Multiline editText внутри scrollview.
Внутри прокрутки, если ваш EditText содержит больше текста, вы не сможете прокрутить внутри, скорее будет прокручиваться вся страница.
Вы можете достичь этого с помощью TouchListener.
edtDescription = (EditText)findViewById(R.id.edt_description);
edtDescription.setVerticalScrollBarEnabled(true);
edtDescription.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
edtDescription.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
edtDescription.setMovementMethod(ScrollingMovementMethod.getInstance());
edtDescription.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
if ((motionEvent.getAction() & MotionEvent.ACTION_UP) != 0 && (motionEvent.getActionMasked() & MotionEvent.ACTION_UP) != 0)
{
view.getParent().requestDisallowInterceptTouchEvent(false);
}
return false;
}
});
В редактируемый текст просто добавьте
android:inputType="textMultiLine"
android:maxLines="3">
Пробовал с?:
<EditText
android:id="@+id/edt_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:maxLines="3"
>
Примените этот фрагмент кода... вы правильно выполнили свое требование. И одна вещь, которую нужно отметить, вы должны дать жестко заданную высоту ScrollView... Я даю здесь как 70dip.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="70dip" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="false" />
</ScrollView>
Если строки textview превышают две строки, scrollview будет прокручиваться
if (mTvContacts.getLayout().getLineCount() >= 2) {
if (scrollContactsHeight == 0)
scrollContactsHeight = scrollContacts.getHeight();
scrollContacts.getLayoutParams().height = scrollContactsHeight * 2;
} else {
if (scrollContactsHeight == 0)
scrollContacts.getLayoutParams().height = scrollContacts.getHeight();
else
scrollContacts.getLayoutParams().height = scrollContacts.getHeight() / 2;
}
<ScrollView
android:id="@+id/scrollContacts"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_toRightOf="@+id/tvTo"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/imgAddContacts">
<TextView
android:id="@+id/tvContacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="wfd"
android:textColor="#000" />
</ScrollView>