【问题标题】:How to create a TextArea in Android如何在 Android 中创建 TextArea
【发布时间】:2020-04-14 20:42:37
【问题描述】:

如何为我的 android 项目显示 TextArea?从 xml 中,唯一的选择是 TextField,多行。但那是可编辑的。我需要一个仅用于显示消息/文本的 TextArea,用户不能编辑/输入。

【问题讨论】:

  • 尝试使用EditText查看-EditText documentation
  • 您需要类似 EditText 字段或 TextView 的样式?
  • 为此目的使用文本视图最适合您的情况。
  • textview 的问题我发现它显示了我的背景是什么,我的图像使阅读变得困难
  • 使用TextView的背景色,解决你的问题。

标签: android


【解决方案1】:

试试这个:

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

【讨论】:

  • 通用语言是英语。
【解决方案2】:

ScrollView 中使用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>

【讨论】:

  • 如何在其中插入多行描述?
【解决方案3】:

如果您不想让用户输入文本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" />

编辑:将下面的属性添加到textView 将使其成为不可编辑的textArea

android:lines="8"
android:maxLines="10"
android:minLines="6" // optional

【讨论】:

    【解决方案4】:

    真的很简单,用XML写这段代码:

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

    【讨论】:

      【解决方案5】:

      ScrollView 中使用TextView 以显示任何行数的消息。用户不能像在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);
      

      【讨论】:

      • 让我试试看效果如何
      • 是的,它非常符合我的要求。现在我如何改变它的字体/大小/颜色?默认文本似乎是灰色褪色的,可见性很差。
      • 我应该把 textarea.setTextSize(20); ,它看起来不像是用于 xml 的。
      • 不,我不是,我正在使用 Eclipse。
      • 是的,我现在使用的是 textview 而不是 edittext。你能看看我的其他问题吗?
      【解决方案6】:
      <EditText
          android:id="@+id/comments_textbox"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="comments"
          android:inputType="textMultiLine"
          android:longClickable="false" />
      

      用它来创建多行文本框,例如 Html 中的 textArea

      【讨论】:

        【解决方案7】:
               <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>
        

        【讨论】:

          【解决方案8】:

          通过 inputType=”textMultiline” 定义一个 Android 多行 EditText 字段。不幸的是,文本看起来奇怪地对齐。要解决这个问题,还可以使用重力=“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 />
          

          检查这个vogella blog

          【讨论】:

            【解决方案9】:

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

             <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" />
            

            【讨论】:

              【解决方案10】:

              这个简短的答案可能会对想要做的人有所帮助。 在你的代码中添加这个简单的代码

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

              【讨论】:

                【解决方案11】:
                <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>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-01-05
                  • 2016-12-18
                  • 1970-01-01
                  相关资源
                  最近更新 更多