如何使EditText仅可读和复制,而不可编辑

10

我想将我的编辑框设置为只读,但不可编辑。

用户应该能够从我的编辑框中复制,但不能由用户进行编辑。

请告诉我如何做到这一点。


1
你可以使用 android:enabled="false",但它无法被复制。 - pavanmvn
7个回答

15

命令text.setTextIsSelectable(true)需要API 11。对于使用较低API的用户,请使用以下XML:

android:inputType="none"
android:textIsSelectable="true"

这将使您的editText可选,但不可编辑。


6
最简单的方法是添加以下代码:
textInput.setInputType(InputType.TYPE_NULL);
textInput.setTextIsSelectable(true);
textInput.setKeyListener(null);

1
简单易用。我在XML中设置了 InputTypeTextIsSelectable 属性,然后在代码中将监听器设置为 null - W0lfw00ds

1
按照其他答案所示,创建一个 TextView,而不是 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)即可。


0

为什么不是这个?

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setEnabled(false);

免责声明:我不是安卓开发者(Xamarin开发者)。我认为这将防止选择和复制文本。 - Ashi

0

你可以覆盖键监听器,这样你就可以做任何事情,除了编辑。

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        return true;
    }
});

0
我正在使用这个类。
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;
    }
}

0
在您的布局视图文件中,使用android:editable="false"属性来设置EditText不可编辑。

此选项将禁用复制功能。 - Toda Raba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接