Wyłączanie EditText w Androidzie

W mojej aplikacji mam EditText, że użytkownik ma tylko dostęp do odczytu, a nie do zapisu.

W kodzie i set android:enabled="false".

Chociaż tło EditText zmieniło się na ciemne, po kliknięciu na niego pojawia się klawiatura i mogę zmienić tekst.

Co należy ustawić, aby wyłączyć EditText?

Author: Adrian Mole, 2010-11-28

26 answers

Uważam, że poprawne byłoby ustawienie android:editable="false".

A jeśli zastanawiasz się, dlaczego mój link wskazuje na atrybuty TextView, to odpowiedź brzmi:EditText inherits from TextView:

EditText to cienka okleina nad TextView, który konfiguruje się jako edytowalny.

Update:
Jak wspomniano w komentarzach poniżej, editable jest przestarzały (od poziomu API 3 ). Zamiast tego powinieneś używać inputType (z wartość none).

 264
Author: Julian,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-06 09:17:36

Użyj EditText.setFocusable(false), aby wyłączyć edycję
EditText.setFocusableInTouchMode(true) aby włączyć edycję;

 174
Author: Sean Android,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-20 14:34:07

Możesz wypróbować następującą metodę:

 private void disableEditText(EditText editText) {
    editText.setFocusable(false);
    editText.setEnabled(false);
    editText.setCursorVisible(false);
    editText.setKeyListener(null);
    editText.setBackgroundColor(Color.TRANSPARENT); 
 }

Enabled EditText:

Enabled EditText

Disabled EditText:

Wyłączony EditText

To działa dla mnie i mam nadzieję, że ci pomoże.
 66
Author: Maksym Kalin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-03 14:51:04

Użyj tego, aby wyłączyć wejście użytkownika

android:focusable="false"

Android: editable= "false" ta metoda jest przestarzała.

 64
Author: Sampath Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-02 12:01:55

Dla wyłączenia edycji EditText, myślę, że możemy użyć focusable lub enable ale

  1. Za pomocą android:enabled=... lub editText.setEnabled(...)

    To również zmienia kolor tekstu w EditText Na szary.
    Po kliknięciu nie ma efektu

  2. Używając android:focusable=... lub editText.setFocusable(false) - editText.setFocusableInTouchMode(true)

    To nie zmienia koloru tekstu EditText
    Po kliknięciu podświetla EditText dolną linię dla około kilku milisekunda

Wyjście

Tutaj wpisz opis obrazka

 43
Author: Phan Van Linh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-10-16 07:38:24

Jako {[1] } jest deprymowane.Możesz użyć InputType TYPE_NULL na EditText

Użyj tak:

editText.setInputType(InputType.TYPE_NULL);
 19
Author: Bhargav Thanki,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-13 06:18:25

As android:editable="false" deprecated In xml

Użyj {[3] }to proste. Po co używać więcej kodu?

Jeśli chcesz w java class Możesz również użyć tego programowo

 editText.setEnabled(false);
 18
Author: android_jain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-29 06:04:43

Po Prostu:

editText.setEnabled(false);
 8
Author: Oded Breiner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-07 11:39:44

Ustaw poniżej właściwości w klasie:

editText.setFocusable(false);
editText.setEnabled(false);

Będzie działać płynnie, jak trzeba.

 5
Author: Rank,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-30 16:56:16

Aby wyłączyć funkcjonalność EditText, wystarczy użyć:

EditText.setInputType(InputType.TYPE_NULL);

Jeśli chcesz włączyć go w jakiś sposób, możesz użyć:

EditText.setInputType(InputType.TYPE_CLASS_TEXT);
 5
Author: Suri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-07-14 21:36:30

Używam google nowo wydany Material Design Library. W moim przypadku działa, gdy używam android: focusable = "false" i android: cursorVisible= "false"

<com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/to_time_input_layout"
                    app:endIconMode="custom"
                    app:endIconDrawable="@drawable/ic_clock"
                    app:endIconContentDescription="ToTime"
                    app:endIconTint="@color/colorAccent"
                    style="@style/OutlinedEditTextStyle"
                    android:hint="To Time">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/to_time_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false"
                        android:cursorVisible="false" />

</com.google.android.material.textfield.TextInputLayout>
 5
Author: Aminul Haque Aome,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-29 04:54:37

Jeśli używasz android:editable="false", eclipse przypomni Ci ten Komunikat "android: editable is deprecated: Use inputType instead".

Więc używam android:focusable="false" zamiast tego, to działało dobrze dla mnie.

 4
Author: wangzhengyi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-23 15:55:17
android:editable="false"

Jest obecnie przestarzałe i używa

 YourEditText.setInputType(InputType.TYPE_NULL);
 4
Author: emkarachchi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-12-11 11:26:02

Disable = FOCUS+CLICK+CURSOR

Wyłączanie ostrości, klikania i widoczności kursora załatwia sprawę.

Oto kod w XML

<EditText
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:cursorVisible="false"
    android:clickable="false"
    />
 4
Author: Rohit Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55

Zamiast tego użyj TextView.

 3
Author: cx0der,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-11-28 16:41:07

Spowoduje to wyłączenie Twojego edittext .

editText.setEnabled(false);

I używając tego

editText.setInputType(InputType.TYPE_NULL);

Sprawi, że Twój Edittext nie pokaże twojego softkeyboard, ale jeśli jest podłączony do fizycznej klawiatury, pozwoli Ci pisać.

 3
Author: Mark Joshua Fajardo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-11-16 03:07:18

W moim przypadku potrzebowałem mojego EditText do przewijania tekstu, jeśli nie. linii przekracza maxLines, gdy jest wyłączona. Ta realizacja zadziałała dla mnie doskonale.

private void setIsChatEditTextEditable(boolean value)
{
    if(value)
    {
        mEdittext.setCursorVisible(true);
        mEdittext.setSelection(chat_edittext.length());
       // use new EditText(getApplicationContext()).getKeyListener()) if required below
        mEdittext.setKeyListener(new AppCompatEditText(getApplicationContext()).getKeyListener());  
    }
    else
    {
        mEdittext.setCursorVisible(false);
        mEdittext.setKeyListener(null);
    }
}
 1
Author: Rishabh876,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-20 07:39:04

Spróbuj tego, działa dobrze dla mnie:

public class CustomEdittext extends EditText {

Boolean mIsTextEditor=true;
public CustomEdittext(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onCheckIsTextEditor() {
    // TODO Auto-generated method stub
    return mIsTextEditor;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    mIsTextEditor=false;
    Boolean mOnTouchEvent=super.onTouchEvent(event);
    mIsTextEditor=true;     
    return mOnTouchEvent;
} }

Uwaga: musisz dodać this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); do swojej aktywności albo keyboardbędzie popup za pierwszym razem.

 1
Author: Favas Kv,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-03-29 05:58:45

Jak wspominają niektóre odpowiedzi, jeśli wyłączysz editText staje się szary i jeśli ustawisz Focusable false kursor będzie wyświetlany.

Jeśli chcesz to zrobić tylko z xml to zrobił sztuczkę

       <YourFloatLabel
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <EditText
                android:id="@+id/view_ads_search_select"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusable="true"
                android:clickable="true"/>  
       </YourFloatLabel>

Po prostu dodaję FrameLayout pojawiający się nad editText i ustawiam go focusable i clickable, aby nie można było kliknąć editText.

 1
Author: Vodet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-04-23 08:21:17

To działa dla mnie:

Android: focusable= "false"

 0
Author: Annie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-27 20:23:05

From @ Asymptote ' s comment on the accepted answer, use:

myEditText.setEnabled(false);
myEditText.setInputType(InputType.TYPE_NULL);

...a Bob jest Twoim wujkiem.

 0
Author: Tom Howard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-28 18:32:42

Dzisiaj nadal używam editable="false", ale także z focusable="false".

Myślę, że musimy uczynić EditText nieedytowalnym, ponieważ chcemy zachować jego styl EditText (z podkreśleniem, podpowiedzią, itp.), ale akceptuje on inne wejścia zamiast tekstu. Na przykład lista rozwijana.

W takim przypadku musimy mieć EditText klikalny (dlatego {[3] } nie jest odpowiedni). Ustawienie focusable="false" wykonaj ten trick, jednak nadal mogę długo trzymać EditText i wkleić na niego własny tekst z Schowek . W zależności od kodu i obsługi może to nawet spowodować awarię aplikacji.

Więc użyłem również editable="false" i teraz wszystko jest Świetne, z wyjątkiem Ostrzeżenia.

 0
Author: Sira Lam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-28 07:51:25

Możesz użyć android:focusable="false", ale musisz również wyłączyć kursor w przeciwnym razie funkcja kopiuj / wklej nadal będzie działać.

Więc użyj

android:focusable="false"
android:cursorVisible="false"
 0
Author: M.Usman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-13 09:07:12

Istnieje wiele było jak osiągnąć wiele poziomów niepełnosprawnych.

  • editText.setShowSoftInputOnFocus(false); i editText.setFocusable

Zapobiega wyświetlaniu EditText zapisu klawiatury w jakimś tekście. Ale kursor jest nadal widoczny i użytkownik może wkleić w jakiś tekst.

  • editText.setCursorVisible(false)

Ukrywa kursor. Nie wiem, dlaczego chcesz to zrobić. Użytkownik może wprowadzić tekst i wkleić.

  • editText.setKeyListener(null)
To dla mnie najwygodniejsze. Nie ma sposobu, w jaki użytkownik może tekst wejściowy, ale widżet nadal działa z OnClickListener, jeśli chcesz wywołać akcję, gdy użytkownik dotknie go
  • editText.setEnabled(false);

Całkowicie wyłącza EditText. Jest dosłownie "tylko do odczytu", użytkownik nie może wprowadzić do niego żadnego tekstu i (na przykład) OnClickListener nie działa.

TextEdit dokumentacja

 0
Author: DeepBlue,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-02-09 19:52:48

Ustaw to w kodzie XML, to działa.

Android: focusableInTouchMode= "false"

 0
Author: Sujeet Kumar Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-04-24 09:36:27

Używanie {[0] } jest przestarzałe. Zamiast tego musisz użyć android:focusable="false"

 0
Author: Boiii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-03 18:14:05