Make EditText behave as a TextView in code

后端 未结 8 2243
暗喜
暗喜 2020-12-29 13:39

How to make an EditText look like a TextView that is done in Java but not in XML..

I have seen how to do it using xml file like

style=\"@android:styl         


        
相关标签:
8条回答
  • 2020-12-29 14:00

    Make EditText behave as a TextView in code

    try this In your layout, set:

        <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 讨论(0)
  • 2020-12-29 14:01

    EditText is a subclass of TextView - so except for editing, what applies to TextView applies to EditText as well.

    0 讨论(0)
  • 2020-12-29 14:09

    Make EditText behave as a TextView in code

    try this

            android:longClickable="false"
            android:clickable="false"    
            android:cursorVisible="false"
            android:editable="false"
    
    0 讨论(0)
  • 2020-12-29 14:09

    Just add an border to EditText : First Of all add an border.xml in Resources/Drawable

    <?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>
    

    Second step : add android:background="@drawable/border" to 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" />
    
    0 讨论(0)
  • 2020-12-29 14:12

    add below two line into your edittext xml:

    android:focusable="false"
    android:inputType="none"
    
    0 讨论(0)
  • 2020-12-29 14:13

    Well I know this was a very old question, but I have to do this one of my apps and came up with a simple approach deduced from all the answers from Google/stack overflow. Suppose u want to make a edit text look like a text view and on button click make it editable, the procedure is as follows:

    In your layout, set:

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

    And in your .java set the color of the text by:

    yourEditText.setTextColor(Color.parseColor("#000000"));
    

    before entering the onClick of button. And in the onClick of button:

    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
    

    Now to make the edit text show again as a textView, on the onClick of another button:

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

    My answer covers two or three questions combined, but I hope it'll be helpful.

    0 讨论(0)
提交回复
热议问题