Make EditText ReadOnly

前端 未结 22 1242

I want to make a read-only EditText view. The XML to do this code seems to be android:editable=\"false\", but I want to do this in code.

H

相关标签:
22条回答
  • 2020-12-04 19:03

    This works for me:

    EditText.setKeyListener(null);
    
    0 讨论(0)
  • Please use this code..

    Edittext.setEnabled(false);
    
    0 讨论(0)
  • 2020-12-04 19:04

    I had no problem making EditTextPreference read-only, by using:

    editTextPref.setSelectable(false);
    

    This works well when coupled with using the 'summary' field to display read-only fields (useful for displaying account info, for example). Updating the summary fields dynamically snatched from http://gmariotti.blogspot.com/2013/01/preferenceactivity-preferencefragment.html

    private static final List<String> keyList;
    
    static {
        keyList = new ArrayList<String>();
        keyList.add("field1");
        keyList.add("field2");
        keyList.add("field3");
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    
        for(int i=0;i<getPreferenceScreen().getPreferenceCount();i++){
            initSummary(getPreferenceScreen().getPreference(i));
        }
    }
    
    private void initSummary(Preference p) {
        if (p instanceof PreferenceCategory) {
            PreferenceCategory pCat = (PreferenceCategory) p;
            for (int i = 0; i < pCat.getPreferenceCount(); i++) {
                initSummary(pCat.getPreference(i));
            }
        } else {
            updatePrefSummary(p);
        }
    }
    
    private void updatePrefSummary(Preference p) {
        if (p instanceof ListPreference) {
            ListPreference listPref = (ListPreference) p;
            p.setSummary(listPref.getEntry());
        }
        if (p instanceof EditTextPreference) {
            EditTextPreference editTextPref = (EditTextPreference) p;
            //editTextPref.setEnabled(false); // this can be used to 'gray out' as well
            editTextPref.setSelectable(false);
            if (keyList.contains(p.getKey())) {
                p.setSummary(editTextPref.getText());
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 19:06

    android:editable="false" has been deprecated. Therefore you cant use it to make the edit text readonly.

    I have done this using the bellow solution. Here I have used

    android:inputType="none"

    android:textIsSelectable="true"

    android:focusable="false"

    Give it try :)

    <EditText
             android:id="@+id/et_newsgpa_university"
             android:layout_height="wrap_content"
             android:layout_width="match_parent"
             android:hint="@string/hint_educational_institute"
             android:textSize="@dimen/regular_text"
             android:inputType="none"
             android:textIsSelectable="true"
             android:focusable="false"
             android:maxLines="1"
             android:imeOptions="actionNext"/>
    
    0 讨论(0)
  • 2020-12-04 19:08

    Try using

    editText.setEnabled(false);
    editText.setClickable(false);
    
    0 讨论(0)
  • 2020-12-04 19:10

    If you setEnabled(false) then your editText would look disabled (gray, etc). You may not want to change the visual aspect of your editor.

    A less intrusive way would be to use setFocusable(false).

    I believe that this answers your question closer to your initial intent.

    0 讨论(0)
提交回复
热议问题