好吧,我知道这是一个非常古老的问题,但我必须在我的应用程序中执行此操作,并从 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);`
我的回答涵盖了两个或三个问题的组合,但我希望它会有所帮助。