如何在Android中创建文本区域

如何在Android中创建文本区域,android,Android,如何显示android项目的文本区域?从xml中,唯一的选择是文本字段,多行。但这是可编辑的。我需要一个文本区,它仅用于显示消息/用户无法编辑/输入文本。它非常简单,请用XML编写以下代码: <EditText android:id="@+id/fname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="First Name" /&

如何显示android项目的文本区域?从xml中,唯一的选择是文本字段,多行。但这是可编辑的。我需要一个文本区,它仅用于显示消息/用户无法编辑/输入文本。

它非常简单,请用XML编写以下代码:

<EditText
    android:id="@+id/fname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="First Name"
/>


滚动视图
中使用
文本视图
,以显示任何行数的消息。用户不能像在
EditText
中那样编辑此视图中的文本

我认为这符合你的要求。试试看

仅当您希望按以下方式修复XML文件中的默认颜色和文本大小时,才可以更改它们:

<TextView 
    android:id="@+id/tv"
    android:layout_width="fill_parent"
    android:layout_height="100px"
    android:textColor="#f00"
    android:textSize="25px"
    android:typeface="serif"
    android:textStyle="italic"/>
TextView textarea = (TextView)findViewById(R.id.tv);  // tv is id in XML file for TextView
textarea.setTextSize(20);
textarea.setTextColor(Color.rgb(0xff, 0, 0));
textarea.setTypeface(Typeface.SERIF, Typeface.ITALIC);

滚动视图中使用
TextView

<ScrollView
    android:id="@+id/ScrollView01"
    android:layout_width="wrap_content"
    android:layout_height="150dip">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</ScrollView>

试试这个:

<EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:inputType="text|textMultiLine"
        android:gravity="top"/>

这个简短的答案可能会帮助那些希望这样做的人。 在代码中添加此简单代码

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:editable="false"
    android:focusable="false"
    android:singleLine="true" />

通过inputType=“textMultiline”定义Android多行编辑文本字段。不幸的是,文本看起来奇怪地对齐。要解决此问题,请使用gravity=“left | top”属性

<EditText
   android:id="@+id/editText1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:ems="10"
   android:gravity="left|top"
   android:inputType="textMultiLine" >

   <requestFocus />


如果您不想允许用户输入文本,请选中此项。
TextView
是此处的最佳选项。您还可以为此添加
EditText
。下面是一个示例代码

如果文本多于指定行,则会自动显示滚动条

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:lines="8"
    android:maxLines="10"
    android:minLines="6"
    android:scrollbars="vertical" />

使用它在Html中创建多行文本框,如textArea
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="100dp">
<EditText
    android:id="@+id/question_input"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"
    android:inputType="text|textMultiLine"
    android:lineSpacingExtra="5sp"
    android:padding="5dp"
    android:textAlignment="textEnd"
    android:typeface="normal" />

</android.support.v4.widget.NestedScrollView>

所有答案都很好,但并不完整。用这个

 <EditText
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_marginTop="12dp"
      android:layout_marginBottom="12dp"
      android:background="@drawable/text_area_background"
      android:gravity="start|top"
      android:hint="@string/write_your_comments"
      android:imeOptions="actionDone"
      android:importantForAutofill="no"
      android:inputType="textMultiLine"
      android:padding="12dp" />


让我试一试,看看效果如何。这非常符合我的要求。现在如何更改其字体/大小/颜色?默认文本似乎是淡灰色的,可见性很差。我应该将textarea.setTextSize(20),它看起来不像是用于xml的。不,我不是,我正在使用eclipse。是的,我现在使用的是textview而不是edittext。你能检查一下我的其他问题吗?我如何在其中插入多行描述?通用语言是英语。尝试使用
EditText
view-。你需要像EditText字段或TextView这样的样式。为此,使用Text view最适合你的情况。TextView的问题我发现它显示了我的想法背景,我有图像使阅读困难的背景颜色文本视图,它解决了你的问题。
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="100dp">
<EditText
    android:id="@+id/question_input"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ems="10"
    android:inputType="text|textMultiLine"
    android:lineSpacingExtra="5sp"
    android:padding="5dp"
    android:textAlignment="textEnd"
    android:typeface="normal" />

</android.support.v4.widget.NestedScrollView>
       <EditText
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:ems="10"
            android:gravity="left|top"
            android:hint="Write your comment.."
            android:inputType="textMultiLine"
            android:textSize="15sp">
            <requestFocus />
       </EditText>
 <EditText
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_marginTop="12dp"
      android:layout_marginBottom="12dp"
      android:background="@drawable/text_area_background"
      android:gravity="start|top"
      android:hint="@string/write_your_comments"
      android:imeOptions="actionDone"
      android:importantForAutofill="no"
      android:inputType="textMultiLine"
      android:padding="12dp" />