如何在不应该​​编辑的edittext中设置文本

How to set text in edittext which should not editable

我正在使用 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" />

My Question is how to put TextView in EditText?

不可能,因为这不是 EditText

的内置行为

How is it possible?

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

示例代码

<?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>

输出

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

android:inputType="none"

如果您想以编程方式进行

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

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