Android: zmusić EditText do usunięcia Fokusa? [duplikat]
To pytanie ma już odpowiedź tutaj:
Chciałbym móc usunąć fokus z EditText. Na przykład, Jeśli pojawi się Klawiatura, a użytkownik ukryje ją przyciskiem wstecz, chciałbym, aby fokus i kursor zniknęły. Jak można to zrobić?
19 answers
Możesz dodać to do onCreate
, A to ukryje klawiaturę za każdym razem, gdy uruchomi się activty.
Można również programowo zmienić fokus na inny element.
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
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-07-18 14:09:03
Możesz sprawić, że kursor i fokus znikną przez
edittext.clearFocus();
Ale Wykryj, kiedy ukrycie tablicy kluczy jest ciężką pracą.
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
2011-05-25 05:59:01
Dodaj LinearLayout
przed EditText
do swojego XML.
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:layout_width="0px"
android:layout_height="0px" />
Lub możesz zrobić to samo, dodając te linie do wyświetlenia przed 'EditText'.
<Button
android:id="@+id/btnSearch"
android:layout_width="50dp"
android:layout_height="50dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:text="Quick Search"
android:textColor="#fff"
android:textSize="13sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:gravity="left"
android:hint="Name"
android:maxLines="1"
android:singleLine="true"
android:textColorHint="@color/blue"
android:textSize="13sp"
android:textStyle="bold" />
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-06-15 09:23:22
Usuń fokus, ale pozostań fokusowalny:
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
EditText straci koncentrację, ale może ją ponownie zyskać na nowym zdarzeniu dotykowym.
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-13 19:18:45
Usuń autofocus edittext android
It ' s working for me
Edytuj w linku sugerują użycie LinearLayout, ale prosty widok będzie działał
<View
android:id="@+id/focus_thief"
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true" />
Wtedy, jeśli ten "złodziej" zostanie umieszczony na górze układu (jako pierwszy element fokusowalny), wywołania do clearFocus()
będą dział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
2014-10-14 12:53:02
Dodaj te dwie właściwości do układu nadrzędnego (np. układ liniowy, układ względny)
android:focusable="true"
android:focusableInTouchMode="true"
Da radę:)
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-05-14 05:39:00
Możesz również dołączyć android: windowSoftInputMode= "stateAlwaysHidden" do sekcji manifestu akcji.
Jest to równoważne:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Ale w XML sposób.
Dla twojej wiadomości, możesz także ukryć klawiaturę z kodami:
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
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-08-26 22:55:15
Musisz usunąć <requestFocus/>
Jeśli go nie używasz i nadal ten sam problem
User LinearLayout
jako rodzic i zestaw
android:focusable="true"
android:focusableInTouchMode="true"
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-02-14 12:51:44
Spróbuj użyć tego na widoku u mnie zadziałało:
<View
android:id="@+id/fucused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"/>
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-20 23:29:45
Dodaj do układu rodzica gdzie umieściłeś EditText
Ten android:focusableInTouchMode="true"
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-10-29 08:58:39
Aby ukryć klawiaturę po rozpoczęciu aktywności.. napisz następujący kod w OnCreate ()..
InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
Aby wyczyścić ostrość i usunąć kursor z edittext.....
editText.clearFocus();
editText.setCursorVisible(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-17 12:12:54
To moja pierwsza odpowiedź na SO, więc nie bądź dla mnie zbyt surowy, jeśli są błędy. : D
Jest kilka odpowiedzi pływających wokół SO, ale czuję potrzebę, aby opublikować moje kompletne rozwiązanie, ponieważ doprowadzało mnie to do szału. Zbierałem kawałki i kawałki z całego świata, więc wybacz, jeśli nie dam każdemu odpowiednich kredytów... :)
(uproszczę mój wynik, bo mój widok ma zbyt wiele elementów i nie chcę spamować z tym i postaram się uczynić go tak ogólnym, jak możliwe...)
Dla Twojego układu potrzebujesz rodzica twój EditText i widok rodzica zdefiniował coś takiego:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lytContainer"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etEditor"
android:inputType="number"
android:layout_gravity="center"
android:hint="@string/enter_your_text"
android:textColor="@android:color/darker_gray"
android:textSize="12dp"
android:textAlignment="center"
android:gravity="center"
android:clickable="true"/>
</LinearLayout>
Więc potrzebowałem tu kilku rzeczy. Musiałem mieć Placeholder dla mojego EditText-czyli to -
Android: hint= "hint"
Również,
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
Stało się, aby EditText nie skupiał się na wprowadzaniu aktywności , a później w samej aktywności podczas ustawiania to ustawienie pomaga, więc możesz ustawić onTouchListener na nim, aby ukraść fokus z EditText .
Teraz w aktywności:
package com.at.keyboardhide;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnTouchListener{
private EditText getEditText;
private LinearLayout getLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.keyboardmain);
getEditText = (EditText)findViewById(R.id.etEditor);
getLinearLayout = (LinearLayout)findViewById(R.id.lytContainer);
getLinearLayout.setOnTouchListener(this);
getEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Log.d("EDTA", "text was entered.");
getEditText.clearFocus();
imm.hideSoftInputFromWindow(barcodeNo.getWindowToken(), 0);
return true;
}
return false;
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v==getLinearLayout){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getEditText.getWindowToken(), 0);
getEditText.clearFocus();
return true;
}
return false;
}
}
Kilka odpowiedzi na bity, które znalazłem na tej stronie z pytaniami, oraz część z rozwiązaniem aktywności, którą znalazłem na tym blogu . Resztę przegapiłem, którą sam musiałem rozgryźć, to wyczyszczenie Focusa na EditText który dodałem zarówno do wewnątrz setOnEditorActionListener i onTouchLister dla widok rodzica .
Mam nadzieję, że to komuś pomoże i zaoszczędzi jego czas. :) Pozdrawiam, Z.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-03-18 13:11:27
W komentarzach zapytałeś, czy można skupić się na innym widoku zamiast EditText. Tak, może. Użyj metody .requestFocus()
dla widoku, który ma być skupiony na początku (w metodzie onCreate ())
Również skupienie innych widoków spowoduje wycięcie pewnej ilości kodu. (kod do ukrycia klawiatury na przykład)
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
2011-07-26 15:49:47
Możesz również dołączyć android:windowSoftInputMode="stateAlwaysHidden"
do sekcji manifestu akcji.
Jest to równoważne:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Miałem ten sam problem. To sprawiło, że oszalałem.
Miałem rozszerzone okno dialogowe z przewijaniem, które miało TableLayout z rozszerzonym LinearLayout, który zawierał SeekBar i EditText.
Pierwszy EditText zawsze miał autofokus po wyświetleniu okna dialogowego i po zakończeniu edycji tekstu nad klawiaturą EditText nadal miał ostrość, a klawiatura była nadal widoczna.
Wypróbowałem prawie wszystkie rozwiązania tego wątku i żadne nie zadziałało.Więc oto moje proste rozwiązanie : (text = EditText)
text.setOnEditorActionListener( new OnEditorActionListener( ){
public boolean onEditorAction( TextView v, int actionId, KeyEvent event ){
if( (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) ||
(actionId == EditorInfo.IME_ACTION_DONE) ){
text.clearFocus( );
InputMethodManager iMgr = null;
iMgr = (InputMethodManager)mContext.getSystemService( Context.INPUT_METHOD_SERVICE );
iMgr.hideSoftInputFromWindow( text.getWindowToken(), 0 );
}
return true;
}
});
Przy okazji ja nie użyłem żadnego z poniższych fragmentów, aby go rozwiązać:
//setFocusableInTouchMode( true )
//setFocusable( true )
//setDescendantFocusability( ViewGroup.FOCUS_BEFORE_DESCENDANTS )
I nie użyłem elementu dystansowego, takiego jak widok o szerokości i wysokości 1DP.
Mam nadzieję, że komuś to pomoże: d
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-08-21 11:49:21
editText.setFocusableInTouchMode(true)
EditText
będzie w stanie uzyskać ostrość, gdy użytkownik dotknie go.
Gdy główny układ (aktywność, okno dialogowe itp.) staje się widoczny EditText
nie otrzymuje automatycznie Fokusa, mimo że jest to pierwszy widok w układzie.
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-10-20 15:20:11
EditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(URLText.getWindowToken(), 0);
EditText.setFocusable(false);
EditText.setFocusableInTouchMode(true);
return true;
} else {
return 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-07-21 14:56:00
Możesz uniknąć skupienia się na swoich elementach, ustawiając atrybut android: descendantFocusability elementu nadrzędnego.
Oto przykład:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search__scroller"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
Tutaj atrybut android: descendantFocusability ustawiony na "blocksDescendants" blokuje koncentrację na elementach potomnych.
Więcej informacji znajdziesz tutaj .
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-07-13 02:59:37
check your xml file
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" >
**<requestFocus />**
</EditText>
//Remove **<requestFocus />** from xml
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-07 12:23:32