我想将我的编辑框设置为只读,但不可编辑。
用户应该能够从我的编辑框中复制,但不能由用户进行编辑。
请告诉我如何做到这一点。
我想将我的编辑框设置为只读,但不可编辑。
用户应该能够从我的编辑框中复制,但不能由用户进行编辑。
请告诉我如何做到这一点。
命令text.setTextIsSelectable(true)
需要API 11。对于使用较低API的用户,请使用以下XML:
android:inputType="none"
android:textIsSelectable="true"
这将使您的editText
可选,但不可编辑。
textInput.setInputType(InputType.TYPE_NULL);
textInput.setTextIsSelectable(true);
textInput.setKeyListener(null);
InputType
和 TextIsSelectable
属性,然后在代码中将监听器设置为 null
。 - W0lfw00dsTextView
,而不是 EditText
。然后在您的 Activity
类中覆盖 Activity 的上下文菜单,如下所示:public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Copy");
//cast the received View to TextView so that you can get its text
TextView yourTextView = (TextView) v;
//place your TextView's text in the clipboard
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(yourTextView.getText());
}
然后在onCreate()
中调用registerForContextMenu(yourTextView)
即可。
为什么不是这个?
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setEnabled(false);
你可以覆盖键监听器,这样你就可以做任何事情,除了编辑。
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true;
}
});
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.EditText;
/*
*
* To make EditText read and copy only but not editable
* using
* sendReadOnlyCallback(callback);
*
*/
public class MyEditText extends EditText {
private InputFilter[] originalFilters = null;
private boolean internalChange = false;
private InputFilter[] myInputFilters = null;
private static ReadonlyCallback sDummyCallback = new ReadonlyCallback() {
@Override
public boolean isReadOnly() {
return false;
}
};
private ReadonlyCallback callback = sDummyCallback;
public MyEditText(Context context) {
super(context);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public interface ReadonlyCallback {
public boolean isReadOnly();
}
public void setReadonlyCallback(ReadonlyCallback cb) {
if (cb == null)
callback = sDummyCallback;
else
callback = cb;
}
public void setFilters(InputFilter[] filters) {
// duplicated from TexView
originalFilters = new InputFilter[filters.length];
System.arraycopy(filters, 0, originalFilters, 0, filters.length);
// funny No. 1 : have to re instantiate `callback`
// otherwise got `NullPointerExcection` when called from `filter`
callback = sDummyCallback;
myInputFilters = new InputFilter[] { new InputFilter() {
// funny No. 2:
// have to make refs to `originalfilters`
// otherwise got `NullPointerExcection` when called from `filter`
InputFilter[] flts = originalFilters;
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (!internalChange && callback.isReadOnly())
return dest.subSequence(dstart, dend);
int filtercount = flts.length;
if (filtercount == 0)
return null;
// duplicated from InputFilter.AllCaps
for (int i = 0; i < filtercount; i++) {
CharSequence repl = flts[i].filter(source, start, end, dest, start, end);
if (repl != null) {
source = repl;
start = 0;
end = repl.length();
}
if (i == filtercount)
return repl;
}
return null;
}
} };
super.setFilters(myInputFilters);
}
@Override
public InputFilter[] getFilters() {
if (myInputFilters == null)
return super.getFilters();
return originalFilters;
}
@Override
public synchronized void setText(CharSequence text, BufferType type) {
internalChange = true;
super.setText(text, type);
internalChange = false;
}
}