让EditText在代码中表现为TextView

9
如何使EditText看起来像TextView,这在Java中可以实现但不适用于XML。
我已经了解到如何使用XML文件来实现此操作。
style="@android:style/Widget.TextView"
android:editable="false"
android:cursorVisible="false"
android:longClickable="false"

但我希望这个功能是用Java完成的,因为我没有使用任何XML文件来布局。 所有的东西都在代码中完成。 我正在尝试在我的代码中使用GestureListener。它对TextView起作用很好,但不适用于EditText。 那么,有什么方法可以让EditText实现GestureEvents吗?
谢谢。

<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="none" android:cursorVisible="false" android:focusable="false" android:singleLine="true"/> - pravingaikwad07
9个回答

10
EditText Et; // initialize your edittext//

Et.setCursorVisible(false);
Et.setLongClickable(false);
Et.setClickable(false);
Et.setFocusable(false);
Et.setSelected(false);
Et.setKeyListener(null);
Et.setBackgroundResource(android.R.color.transparent);

你再也不会将它视为EditText了。


非常感谢!救了我的一天。我做了以下操作: editText.setFocusable(enable); editText.setFocusableInTouchMode(enable); editText.setLongClickable(enable); - Eduardo Reis

4

在EditText的xml文件中添加以下两行:

android:focusable="false"
android:inputType="none"

2

这是将EditText变成TextView的完美方法。

在你的EditText中使用。

<EditText
          android:id="@+id/editTextPassword"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="none"
          android:enabled="false"
          android:focusable="false"
          android:clickable="false"
          android:longClickable="false"
          android:inputType="none"/>

1
OP明确要求以编程方式完成此操作,因为OP已经知道如何在XML中完成。 - Muntashir Akon

2

EditTextTextView的子类,所以除了编辑之外,适用于TextView的内容同样适用于EditText


3
是的,但我正在尝试在EditText上使用手势监听器。它对于TextView很好用,但对于EditText却不行。 - Siva Kumar

1

我知道这是一个很古老的问题,但我必须在我的应用程序中实现它,并从Google / stack overflow的所有答案中得出了一个简单的方法。假设您想使编辑文本看起来像文本视图,并在按钮单击时使其可编辑,则该过程如下:

在您的布局中设置:

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt_postContent"
        android:textSize="17sp"
        android:gravity="top|left"
        android:text="This is a dummy text "
        android:layout_below="@+id/img_empimage"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:enabled="false"
        android:background="@android:color/transparent"
        style="?android:attr/textViewStyle" />

在你的.java文件中,通过以下方式设置文本颜色:
yourEditText.setTextColor(Color.parseColor("#000000"));

在进入按钮的 onClick 之前。而在按钮的 onClick 中:

InputMethodManager imm = (InputMethodManager)               getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(postContent, InputMethodManager.SHOW_IMPLICIT);//shows   keyboard
int position = postContent.length();
Editable text = postContent.getText();
yourEditText.setBackground(getResources().getDrawable(R.drawable.border));//shows a border around your text view making it look like a edit text
yourEditTex.setEnabled(true);//enables the edit text which we disabled in layout file
yourEditTex.setCursorVisible(true);
Selection.setSelection(text,position-1);`//places the cursor at the end of the text

现在要使编辑文本再次显示为textView,需要在另一个按钮的onClick中操作:
String s = postContent.getText().toString();
yourEditText.setText(s);
yourEditText.setEnabled(false);
yourEditText.setTextColor(Color.parseColor("#000000"));
yourEditText.setBackground(null);`

我的回答涵盖了两到三个问题,但我希望它有所帮助。


1
让EditText在代码中表现得像TextView
试试这个
        android:longClickable="false"
        android:clickable="false"    
        android:cursorVisible="false"
        android:editable="false"

0
使EditText在代码中像TextView一样运作
尝试以下步骤: 在布局中设置:
    <EditText
          android:id="@+id/editTextPassword"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="8dp"
          android:layout_marginBottom="8dp"
          android:inputType="none"
          android:imeOptions="actionDone"
          android:enabled="false"
          style="?android:attr/textViewStyle" />

0
这就是我需要创建该效果的全部内容。
在代码中:
ET.setFocusable(false);

还有在布局方面:

android:background="@android:color/transparent"

在布局中设置背景会完全移除下划线,而不仅仅是在代码中隐藏它。

使用以下方法使其可编辑:

ET.setFocusableInTouchMode(true);

0

只需为EditText添加边框: 首先在资源/可绘制文件夹中添加border.xml

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
  <solid android:color="#ffffff" />
  <stroke android:width="1dip" android:color="#000000"/>
</shape>

第二步:在TextView中添加android:background="@drawable/border"

 <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:inputType="date"
                    android:layout_column="1"
                    android:id="@+id/dateFromTextView"
                    android:layout_marginRight="3dp"
                    android:background="@drawable/border"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:layout_gravity="center"
                    android:gravity="center" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接