【问题标题】:Make EditText behave as a TextView in code使 EditText 在代码中表现为 TextView
【发布时间】:2021-09-14 18:56:00
【问题描述】:

如何使 EditText 看起来像一个用 Java 完成但不是用 XML 完成的 TextView..

我已经看到了如何使用 xml 文件来做到这一点

style="@android:style/Widget.TextView"
android:editable="false"
android:cursorVisible="false"
android:longClickable="false"

但我希望这在 java 中完成,因为我没有使用任何 xml 文件进行布局.. 一切都在代码本身中完成.. 我正在尝试在我的代码中使用 GestureListener .. 它适用于 TextView 但不适用于 EditText 那么,要为 EditText 做些什么以便实现 GestureEvents 吗?

谢谢,

【问题讨论】:

标签: android


【解决方案1】:
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);
【解决方案2】:

在您的edittext xml中添加以下两行:

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

【讨论】:

    【解决方案3】:

    EditTextTextView 的子类 - 因此,除了编辑之外,适用于 TextView 的内容也适用于 EditText

    【讨论】:

    • 是的 .. 但我正在尝试在 EditText 上使用手势侦听器 .. 它适用于 TextView 但不适用于 EditText
    【解决方案4】:

    使 EditText 在代码中表现为 TextView

    试试这个

            android:longClickable="false"
            android:clickable="false"    
            android:cursorVisible="false"
            android:editable="false"
    

    【讨论】:

      【解决方案5】:

      好吧,我知道这是一个非常古老的问题,但我必须在我的应用程序中执行此操作,并从 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 of 按钮之前。并在按钮的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
      

      现在让编辑文本在另一个按钮的onClick 上再次显示为textView

      String s = postContent.getText().toString();
      yourEditText.setText(s);
      yourEditText.setEnabled(false);
      yourEditText.setTextColor(Color.parseColor("#000000"));
      yourEditText.setBackground(null);`
      

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

      【讨论】:

        【解决方案6】:

        这是将 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"/>
        

        【讨论】:

          【解决方案7】:

          只需给 EditText 添加一个边框: 首先在 Resources/Drawable 中添加一个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>
          

          第二步: 将 android:background="@drawable/border" 添加到 TextView

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

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:

              这就是我创建效果所需的全部内容。

              在代码中:

              ET.setFocusable(false);
              

              在布局中:

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

              在布局中设置背景完全删除下栏,而不是将其隐藏在代码中。

              使其再次可编辑:

              ET.setFocusableInTouchMode(true);
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-01-26
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多