【问题标题】:How to convert TextView to EditText in android?如何在 Android 中将 TextView 转换为 EditText?
【发布时间】:2010-11-18 11:53:46
【问题描述】:

我想在 android 的按钮单击事件中将 TextView 转换为 Edit Text,反之亦然。我不知道如何实现这一点,如果有人知道解决方案,请帮助。

问候, 拉贾潘迪安.K

【问题讨论】:

    标签: android


    【解决方案1】:

    最好使用 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");
    }
    

    【讨论】:

    • 老实说,我认为在所有这些中,这个答案是最好的。它简单多了。我好奇。什么时候应该使用这个?我阅读了文档,它说的是动画类型的东西。但在这种情况下,使用它是有意义的。那么在这种情况下有理由不使用它吗?
    • 视图切换器运行良好,您可以选择轻松添加动画。
    • 不错,但在 switcher 声明中添加了额外的空间
    • 我看到你使用了 android:onClick="TextViewClicked",但是当我使用 Fragment 时,它会搜索该 Fragment 的活动,因此找不到该方法。我该如何解决这个问题,以便它从 Fragment 而不是父活动中找到方法?
    • @joneswah this 可能对你有些兴趣。
    【解决方案2】:

    也许您可以分别拥有一个并切换它们的可见性。单击按钮时,获取可见的文本,将其应用于不可见的文本,然后交换它们的可见性。

    【讨论】:

    • @MarvinLabs:+1,他更快 ;)
    【解决方案3】:

    您可以在布局中同时使用这两种方法,并且始终只显示其中一种。当 EditText 值发生变化时,更新隐藏的 TextView 以使其保持同步。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        我认为最好的是实际使用 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 View 的背景设为 null。

        这样

        <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"/>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-05-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-08
          • 1970-01-01
          相关资源
          最近更新 更多