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?
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
).
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ę;
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:
Disabled EditText:
To działa dla mnie i mam nadzieję, że ci pomoże.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.
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
-
Za pomocą
android:enabled=...
lubeditText.setEnabled(...)
To również zmienia kolor tekstu w
EditText
Na szary.
Po kliknięciu nie ma efektu -
Używając
android:focusable=...
lubeditText.setFocusable(false)
-editText.setFocusableInTouchMode(true)
To nie zmienia koloru tekstu
EditText
Po kliknięciu podświetlaEditText
dolną linię dla około kilku milisekunda
Wyjście
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);
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);
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);
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.
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);
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>
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.
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);
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"
/>
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
.
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ć.
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);
}
}
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 keyboard
będzie popup za pierwszym razem.
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.
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"
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.
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.
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"
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);
ieditText.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)
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
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"
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"
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