如何使EditText只读取和复制但不可编辑

时间:2014-02-24 11:55:55

标签: android android-edittext

我想让我的编辑框只读但不可编辑。

用户应该可以从我的编辑框中复制,但不能用户编辑。

请让我知道如何做到这一点。

7 个答案:

答案 0 :(得分:13)

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

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

这会使editText可选,但不可编辑。

答案 1 :(得分:4)

最简单的方法是添加以下代码:

textInput.setInputType(InputType.TYPE_NULL);
textInput.setTextIsSelectable(true);
textInput.setKeyListener(null);

答案 2 :(得分:1)

按照其他答案所示,而不是TextView创建EditText。然后覆盖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());
}

然后只需拨打registerForContextMenu(yourTextView);中的onCreate()

答案 3 :(得分:0)

您可以覆盖密钥监听器,因此除了编辑

之外,您可以执行任何操作
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        return true;
    }
});

答案 4 :(得分:0)

对布局视图文件中的EditText使用android:editable =“false”属性。

答案 5 :(得分: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;
    }
}

答案 6 :(得分:0)

为什么不呢?

from zeep import Client
URL="wsdl url"
from zeep import Client
from zeep.transports import Transport
from requests import Session
client= Client(URL)
client = client.service.Login("id","password")