Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

Android: How to make TextView editable?

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

<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 EditText for it look like TextView: without borders and background?

question from:https://stackoverflow.com/questions/12056054/android-how-to-make-textview-editable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I know you don't want to use an EditText but it's really easy to make it look like a 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".

Edit:

The TextView's editable param does make it editable (with some restrictions).

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...