2014-02-24 43 views
6

我想让我的编辑框为只读但不可编辑。如何使EditText只读但不可编辑

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

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

+4

使用一个TextView。 – Blackbelt

+1

你可以使用android:enabled =“false”但它不能被复制 – pavanmvn

回答

0

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

final EditText edittext = (EditText) findViewById(R.id.edittext); 
edittext.setOnKeyListener(new OnKeyListener() { 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     return true; 
    } 
}); 
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()

0

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

+0

这个选项也会禁用复制能力 –

9

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

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

这会让你的editText可选,但是不能编辑。

+0

'android:inputType =“none”'仍然允许编辑。 – BlueMonkMN

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; 
    } 
} 
2

要做到这一点,最简单的方法是添加以下代码:

textInput.setInputType(InputType.TYPE_NULL); 
textInput.setTextIsSelectable(true); 
textInput.setKeyListener(null); 
相关问题