Android에서 EditText를 입력 한 후 키보드를 숨기는 방법은 무엇입니까?
나는이 EditText
부모의 하단에 정렬 버튼을 클릭합니다.
텍스트를 입력하고 버튼을 눌러 데이터를 저장해도 가상 키보드가 사라지지 않습니다.
누구든지 키보드를 숨기는 방법을 안내 할 수 있습니까?
작동합니다.
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
this.getCurrentFocus ()가 null을 반환하지 않는지 확인하십시오.
EditText 내에서 imeOptions를 정의 할 수도 있습니다. 이렇게하면 Done을 누르면 키보드가 사라집니다.
<EditText
android:id="@+id/editText1"
android:inputType="text"
android:imeOptions="actionDone"/>
mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// do something, e.g. set your TextView here via .setText()
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
});
및 xml
android:imeOptions="actionDone"
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
이 방법을 사용하는 사람을 보지 못했습니다.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean focused) {
InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (focused)
keyboard.showSoftInput(editText, 0);
else
keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
});
그런 다음 editText에 포커스를 요청하십시오.
editText.requestFocus();
EditText 액션 리스너에 포함 된 솔루션 :
public void onCreate(Bundle savedInstanceState) {
...
...
edittext = (EditText) findViewById(R.id.EditText01);
edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
return false;
}
});
...
...
}
enter 옵션이 작동하는이 두 줄의 코드를 적어 두십시오.
InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
내 EditText가 입력시 자동으로 해제되지 않았기 때문에 이것을 발견했습니다.
이것은 내 원래 코드였습니다.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {
// Do stuff when user presses enter
return true;
}
return false;
}
});
나는 선을 제거하여 해결했습니다.
return true;
사용자가 Enter 키를 누르면 작업을 수행 한 후.
이것이 누군가를 돕기를 바랍니다.
지난 며칠 동안 이것으로 어려움을 겪었고 정말 잘 작동하는 솔루션을 찾았습니다. 터치가 EditText 외부에서 이루어지면 소프트 키보드가 숨겨집니다.
여기에 게시 된 코드 : Android에서 클릭시 기본 키보드 숨기기
이 방법을 사용하여 편집 텍스트에서 키보드를 제거합니다.
public static void hideKeyboard(Activity activity, IBinder binder) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (binder != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
inputManager.showSoftInputFromInputMethod(binder, 0);
}
}
}
그리고이 방법은 활동에서 키보드를 제거하는 방법입니다 (예를 들어 편집 텍스트가 바인딩 된 키보드로 포커스를 잃었을 때 작동하지 않는 경우도 있습니다. 그러나 다른 상황에서는 잘 작동하고 키보드를 잡는 요소에 신경을 쓰다)
public static void hideKeyboard(Activity activity) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
상단에 표시된 답변을 볼 수 있습니다. 그러나 나는 getDialog().getCurrentFocus()
잘 사용 하고 일했습니다. 이 답변을 게시하면 "this"
oncreatedialog에 입력 할 수 없습니다.
그래서 이것이 제 대답입니다. 표시된 답변을 시도했지만 효과가없는 경우 다음을 시도해 볼 수 있습니다.
InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
int klavStat = 1; // for keyboard soft/hide button
int inType; // to remeber your default keybort Type
편집기-EditText 필드입니다.
/// metod for onclick button ///
public void keyboard(View view) {
if (klavStat == 1) {
klavStat = 0;
inType = editor.getInputType();
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
editor.setInputType(InputType.TYPE_NULL);
editor.setTextIsSelectable(true);
} else {
klavStat = 1;
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
editor.setInputType(inType);
}
}
편집 텍스트 필드가 있으면 포커스 변경을 지켜봐야합니다.
다음과 같이 쉽게 호출을위한 싱글 톤 클래스를 만들 수 있습니다.
public class KeyboardUtils {
private static KeyboardUtils instance;
private InputMethodManager inputMethodManager;
private KeyboardUtils() {
}
public static KeyboardUtils getInstance() {
if (instance == null)
instance = new KeyboardUtils();
return instance;
}
private InputMethodManager getInputMethodManager() {
if (inputMethodManager == null)
inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE);
return inputMethodManager;
}
@SuppressWarnings("ConstantConditions")
public void hide(final Activity activity) {
new Handler().post(new Runnable() {
@Override
public void run() {
try {
getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
}
}
따라서 after는 활동에서 다음 양식을 호출 할 수 있습니다.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
KeyboardUtils.getInstance().hide(this);
}
}
editText.setInputType(InputType.TYPE_NULL);
참고 URL : https://stackoverflow.com/questions/2342620/how-to-hide-keyboard-after-typing-in-edittext-in-android
'programing tip' 카테고리의 다른 글
필드가 업데이트 될 때 UITextField "값 변경됨"이 실행되지 않음 (0) | 2020.11.06 |
---|---|
배포 인증서 / 개인 키가 설치되지 않았습니다. (0) | 2020.11.06 |
주소를 위도 / 경도로 변환하는 방법은 무엇입니까? (0) | 2020.11.05 |
Python object .__ repr __ (self)는 표현식이어야합니까? (0) | 2020.11.05 |
xcode에서 탭을 활성화 하시겠습니까? (0) | 2020.11.05 |