如何允许在Android的EditText视图多行?
当前回答
<EditText
android:hint="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textMultiLine|textNoSuggestions"
android:id="@+id/edittxtAddress"
android:layout_marginLeft="@dimen/textsize2"
android:textColor="@android:color/white"
android:scrollbars="vertical"
android:minLines="2"
android:textStyle="normal" />
并在活动中删除“\n”是用户按下nextline
String editStr= edittxtAddress.getText().toString().trim();
MyString= editStr.replaceAll("\n"," ");
其他回答
这是我从http://www.pcsalt.com/android/edittext-with-single-line-line-wrapping-and-done-action-in-android/上学到的,尽管我自己并不喜欢这个网站。如果你想要多行,但想要保留enter按钮作为发布按钮,将列表视图的“水平滚动”设置为false。
android:scrollHorizontally=“false”
如果它在xml中不起作用,那么以编程方式实现它就可以了。
listView.setHorizontallyScrolling(假);
另一件整洁的事情是使用android:minHeight和android:layout_height="wrap_content"。通过这种方式,您可以在编辑为空时设置编辑的大小,当用户输入内容时,它会根据用户输入的数量进行扩展,包括多行。
试试这个, 将这些行添加到你的编辑文本视图中,我将添加我的。确保你理解它
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
<EditText
android:inputType="textMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_newprom_description"
android:padding="10dp"
android:lines="5"
android:overScrollMode="always"
android:scrollbarStyle="insideInset"
android:minLines="5"
android:gravity="top|left"
android:scrollbars="vertical"
android:layout_marginBottom="20dp"/>
并在你的Java类使点击监听器到这个编辑文本如下,我将添加我的,改变名称根据你的。
EditText description;
description = (EditText)findViewById(R.id.editText_newprom_description);
description.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return false;
}
});
这对我来说很好
禁用之前在主题中分配的行数使用xml属性:android:lines="@null"
所有这些都很好,但如果你的编辑文本在上层滚动视图中就行不通了:)也许最常见的例子是“设置”视图,它有太多的项,以至于超出了可见区域。在这种情况下,你把它们都放在滚动视图,使设置可滚动。如果你在设置中需要多行可滚动编辑文本,它的滚动将不起作用。
推荐文章
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?
- 操作栏和新引入的工具栏有什么不同?
- 调整浮动动作按钮图标大小(fab)
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- android:在触摸移动时移动视图
- 如何以编程方式将ID分配给视图?
- 如何解决INSTALL_FAILED_DEXOPT错误?
- 有没有办法自动安装Android SDK ?
- 如何检查一个视图在Android中是否可见?
- Android是否保留。apk文件?如果有,在哪里?
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 如何检查JSON键是否存在?
- ImageView -有高度匹配宽度?