如何在Android中将TextView转换为EditText?

36
我想在Android中的按钮点击事件中将TextView转换为EditText,反之亦然。我不知道如何实现,请帮忙提供解决方案。
敬礼, Rajapandian.K
5个回答

79

最好使用ViewSwitcher

...
    <ViewSwitcher
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/clickable_text_view"
            android:clickable="true"
            android:onClick="TextViewClicked"
            android:text="@string/some_value" />

        <EditText
            android:id="@+id/hidden_edit_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/some_value" />
    </ViewSwitcher>
...

然后你可以通过以下方式切换视图

public void TextViewClicked() {
    ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.my_switcher);
    switcher.showNext(); //or switcher.showPrevious();
    TextView myTV = (TextView) switcher.findViewById(R.id.clickable_text_view);
    myTV.setText("value");
}

我真诚地认为,在所有答案中,这个答案是最好的。它更加简单。我很好奇。这应该在什么时候使用?我读了文档,它说用于动画类型的东西。但在这种情况下,使用它是有意义的。那么在这种情况下不使用它有什么理由吗? - Andy
视图切换器工作得很好,而且您可以轻松添加动画选项。 - Patrick Jackson
1
不错,但在 switcher 声明中添加额外的空格。 - ivan.mylyanyk
我看到你使用了android:onClick="TextViewClicked",但是由于我正在使用Fragment,它会搜索该Fragment的活动,因此找不到该方法。我该如何修复它,以便它可以从Fragment中找到方法而不是父活动? - edwoollard
@joneswah 这个链接或许对你有些帮助:http://meta.stackoverflow.com/questions/325551/suggested-edit-rejected-because-it-was-intended-to-address-the-author-of-the-po - Neurotransmitter

12

也许你可以准备两个元素并且控制它们的可见性。当按钮被点击时,获取当前可见元素的文本,将其应用于不可见元素,并交换它们的可见性。


3
+1,他更快。 - Peter Knego

5
您可以在布局中同时包含EditText和TextView,但始终只显示其中一个。当EditText的值发生更改时,请更新隐藏的TextView以保持它们同步。

0

我从未尝试过,但你可以(可能)使用EditText并将背景设置为TextView的背景。然后,在onfocus更改时,您可以根据任何给定时刻想要查看的内容简单地交换背景。我会尝试实现它并看看它是否有效。


0

我认为最好的做法是实际上使用 Framelayout 的概念。

这样原始布局就会看起来像

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
.
.
.
</Linearlayout>

将它放在 FrameLayout 中,像这样。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
.
.
.
</Linearlayout>

<!-- The Magic is here -->
<View

    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>



</FrameLayout>

这里的LinearLayout将包含所有的EditText视图。 同时记得将EditText视图的背景设置为空。

像这样

<EditText 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:id="@+id/editInp"
        android:hint="@string/hello_world"
        android:background="@null"
        android:singleLine="true"
        android:inputType="textCapWords"/>

或者

<EditText 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:id="@+id/editInp"
        android:hint="@string/hello_world"
        android:background="@android:drawable/transparent"
        android:singleLine="true"
        android:inputType="textCapWords"/>

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