如何允许在Android的EditText视图多行?
当前回答
这为我工作,实际上这2个属性是重要的:inputType和行。此外,你可能需要一个滚动条,下面的代码显示了如何创建一个:
<EditText
android:id="@+id/addr_edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:inputType="textEmailAddress|textMultiLine"
android:lines="20"
android:minLines="5"
android:scrollHorizontally="false"
android:scrollbars="vertical" />
其他回答
<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"," ");
所有这些都很好,但如果你的编辑文本在上层滚动视图中就行不通了:)也许最常见的例子是“设置”视图,它有太多的项,以至于超出了可见区域。在这种情况下,你把它们都放在滚动视图,使设置可滚动。如果你在设置中需要多行可滚动编辑文本,它的滚动将不起作用。
EditText有singleLine属性。你可以在XML中设置或者调用setSingleLine(false); http://developer.android.com/reference/android/widget/TextView.html#setSingleLine%28%29
试试这个, 将这些行添加到你的编辑文本视图中,我将添加我的。确保你理解它
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;
}
});
这对我来说很好
<EditText
android:id="@id/editText" //id of editText
android:gravity="start" // Where to start Typing
android:inputType="textMultiLine" // multiline
android:imeOptions="actionDone" // Keyboard done button
android:minLines="5" // Min Line of editText
android:hint="@string/Enter Data" // Hint in editText
android:layout_width="match_parent" //width editText
android:layout_height="wrap_content" //height editText
/>
推荐文章
- 在清单中注册应用程序类?
- Android:从数组中编程创建旋转器
- Android命令行工具sdkmanager总是显示:警告:无法创建设置
- 如何设置RecyclerView应用程序:layoutManager=""从XML?
- 在没有开发服务器的情况下在设备上构建和安装unsigned apk ?
- 操作栏和新引入的工具栏有什么不同?
- 调整浮动动作按钮图标大小(fab)
- Android Studio无法找到有效的Jvm(与MAC OS相关)
- android:在触摸移动时移动视图
- 如何以编程方式将ID分配给视图?
- 如何解决INSTALL_FAILED_DEXOPT错误?
- 有没有办法自动安装Android SDK ?
- 如何检查一个视图在Android中是否可见?
- Android是否保留。apk文件?如果有,在哪里?
- 在Android 5棒棒糖中,通知栏图标变成白色