Android:如何使 TextView 可编辑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12056054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:36:25  来源:igfitidea点击:

Android: How to make TextView editable?

android

提问by uncle Lem

How can I allow user to edit a TextView? Of course, I can use EditTextinstead, but I don't know how to customize it and also I've read in Android documentation that TextViewcan be editable. So I tried this:

如何允许用户编辑TextView? 当然,我可以EditText改用,但我不知道如何自定义它,而且我已经阅读了TextView可编辑的Android 文档。所以我试过这个:

<TextView android:id="@+id/tv"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="80sp"
          android:text="MyText"
          android:editable="true"
          android:singleLine="true"
          android:inputType="text"
          android:focusable="true"
          android:clickable="true"
          android:cursorVisible="true"/>

But it still looks like common TextView. Does anyone know what I have missed? Or, may be, how to customize EditTextfor it look like TextView: without borders and background?

但是看起来还是很普通的TextView。有谁知道我错过了什么?或者,可能是,如何自定义EditText它看起来像TextView:没有边框和背景?

回答by Benito Bertoli

I know you don't want to use an EditTextbut it's really easy to make it look like a TextView.

我知道你不想使用 anEditText但让它看起来像一个TextView.

<EditText
     android:id="@+id/Id"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="@android:color/transparent" >
 </EditText>

You can also use android:background="@null".

您也可以使用android:background="@null".

Edit:

编辑:

The TextView's editableparam doesmake it editable (with some restrictions).

TextVieweditablePARAM确实使其可编辑(有一些限制)。

If you set android:editable="true"you can access the TextViewvia the D-pad, or you could add android:focusableInTouchMode="true"to be able to gain focus on touch.

如果您设置,android:editable="true"您可以TextView通过方向键访问,或者您可以添加android:focusableInTouchMode="true"以便能够专注于触摸。

The problem is you cannot modify the existing text, and you cannot move the cursor. The text you write just gets added before the existing text.

问题是您无法修改现有文本,也无法移动光标。您编写的文本只是在现有文本之前添加。

回答by Aerilys

You can fake a editable Textview. You just have to hide the textview when you touch it (make it "clickable"), replace it with an EditText, and display it again when the edit is over.

您可以伪造可编辑的 Textview。您只需要在触摸它时隐藏 textview(使其“可点击”),将其替换为 EditText,并在编辑结束时再次显示它。

回答by Aristo Michael

You can make your TextView editable by adding these lines

您可以通过添加这些行使您的 TextView 可编辑

tv.setFocusable(true);
tv.setEnabled(true);
tv.setClickable(true);
tv.setFocusableInTouchMode(true);

回答by Renascienza

TextView defines all capabilities found on EditText, but doesn't have built-in support to them. Some main differences on EditText:

TextView 定义了 EditText 上的所有功能,但没有对它们的内置支持。EditText 的一些主要区别:

a) Method getDefaultEditable() returns true. This is only a mark that defines this subclass as editable.

a)方法 getDefaultEditable() 返回 true。这只是将此子类定义为可编辑的标记。

b) A movement method. Is an object that control the cursor behavior (position, backward/forward moves - that may change in some languages, etc). In opposition, TextView just returns null, because is not cursor anyway.

b)一种运动方法。是一个控制光标行为的对象(位置、向后/向前移动 - 在某些语言中可能会发生变化等)。相反,TextView 只返回 null,因为无论如何都不是光标。

c) Method CharSequence getText(). TextView returns a single String for that. EditText uses a specific char sequence implementation (Editable) that represents a mutable text buffer.

c)方法 CharSequence getText()。TextView 为此返回单个字符串。EditText 使用表示可变文本缓冲区的特定字符序列实现 (Editable)。

Because that, we can't think about TextView like a restrained EditText. TextView sketch the editoring interface, but not implement itself.

正因为如此,我们不能把 TextView 想成一个内敛的 EditText。TextView 勾勒出编辑界面,但不实现自身。

If you need a text component that you can switch off editing sometimes, you are looking for the EditText component.

如果您需要一个有时可以关闭编辑的文本组件,那么您正在寻找 EditText 组件。

回答by priti

    tv.setCursorVisible(true);
    tv.setFocusableInTouchMode(true);
    tv.requestFocus();
    tv.setEnabled(true);