【问题标题】:How to set text in edittext which should not editable如何在edittext中设置不可编辑的文本
【发布时间】:2018-09-05 05:35:48
【问题描述】:

我正在使用EditText 在左侧显示TextView。这不应该是可编辑的。

我的问题是如何将TextView 放入EditText?怎么可能?

其实我想要这样的:

下面是xml代码。

<EditText
        android:textAlignment="textEnd"
        android:text="user@gmail.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="*Mobile number"
        android:inputType="number"
        android:gravity="end" />

【问题讨论】:

  • 所以你想要*Mobile number 和之后输入数字的选项?
  • 没错@grrigore
  • 您应该将TextView 用于第一个,EditText 用于第二个。无需尝试使 EditText 不可编辑,因为这就是 TextView 的用途。

标签: android xml android-layout android-edittext textview


【解决方案1】:

使用以下属性使您的编辑文本不可编辑:

android:inputType="none"

如果你想以编程方式进行

EditText editText= (EditText) findViewById(R.id.yourid);
editText.setEnabled(false);
editText.setKeyListener(null);

【讨论】:

    【解决方案2】:

    我完全同意@Nilesh Rathod 只是想提议将这个LinearLayout 移动到一个单独的文件中,并在多个地方重复使用它,比如Fragment

    【讨论】:

      【解决方案3】:

      我的问题是如何将 TextView 放入 EditText?

      不可能,因为这不是EditText 的内置行为

      这怎么可能?

      您可以在LinearLayoutandroid:orientation="horizontal" 中使用TextViewEditText

      示例代码

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="?attr/colorBackgroundFloating"
          android:orientation="vertical">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:gravity="center"
              android:orientation="horizontal">
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:padding="5dp"
                  android:text="Enter Name" />
      
              <EditText
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="right"
                  android:text="123"
                  android:padding="5dp" />
      
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:gravity="center"
              android:orientation="horizontal">
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:padding="5dp"
                  android:text="Enter Name" />
      
              <EditText
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="right"
                  android:text="123"
                  android:padding="5dp" />
      
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:gravity="center"
              android:orientation="horizontal">
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:padding="5dp"
                  android:text="Enter Name" />
      
              <EditText
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="right"
                  android:text="123"
                  android:padding="5dp" />
      
          </LinearLayout>
      
      </LinearLayout>
      

      输出

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-28
        • 2011-06-03
        • 1970-01-01
        • 2023-02-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多