Bir düğmeye basıldığında klavyeyi nasıl kapatırım?
Bir düğmeye basıldığında klavyeyi nasıl kapatırım?
Yanıtlar:
Sanal Klavyeyi devre dışı bırakmak mı yoksa kaldırmak mı istiyorsunuz?
Sadece reddetmek istiyorsanız, düğmenizdeki aşağıdaki kod satırlarını kullanabilirsiniz.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
getActivity()) getCurrentFocus().getWindowToken()kullanabilirsiniz hideSoftInputFromWindow(). Ayrıca, etkinlikleri değiştirirken gitmesini sağlamaya çalışıyorsanız onPause()değil, içinde yapın onStop().
Yukarıdaki çözüm tüm cihazlar için çalışmıyor ve ayrıca bir parametre olarak EditText kullanıyor. Bu benim çözümüm, sadece şu basit yöntemi çağırın:
private void hideSoftKeyBoard() {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if(imm.isAcceptingText()) { // verify if the soft keyboard is open
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
isAcceptingText()bu yanıtı diğerlerinden daha iyi yaptı
Bu benim çözümüm
public static void hideKeyboard(Activity activity) {
View v = activity.getWindow().getCurrentFocus();
if (v != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
İşte bir Kotlin çözümü (çeşitli yanıtları ileti dizisinde karıştırın)
Bir uzantı işlevi oluşturun (muhtemelen ortak bir ViewHelpers sınıfında)
fun Activity.dismissKeyboard() {
val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
if( inputMethodManager.isAcceptingText )
inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
}
O zaman şunu kullanarak tüketin:
// from activity
this.dismissKeyboard()
// from fragment
activity.dismissKeyboard()
InputMethodManager ile ilk çözüm benim için bir şampiyon gibi çalıştı, getWindow (). SetSoftInputMode yöntemi android 4.0.3 HTC Amaze'de yoktu.
@Ethan Allen, düzenleme metnini son haline getirmeme gerek yoktu. Belki de içeren yöntemi bildirdiğiniz bir EditText iç sınıfı kullanıyorsunuzdur? EditText'i Aktivitenin bir sınıf değişkeni yapabilirsiniz. Veya sadece iç sınıf / yöntem içinde yeni bir EditText bildirin ve findViewById () öğesini tekrar kullanın. Ayrıca, formdaki hangi EditText'in odaklandığını bilmem gerektiğini bulamadım. Sadece keyfi olarak seçip kullanabilirim. Şöyle:
EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
public static void hideSoftInput(Activity activity) {
try {
if (activity == null || activity.isFinishing()) return;
Window window = activity.getWindow();
if (window == null) return;
View view = window.getCurrentFocus();
//give decorView a chance
if (view == null) view = window.getDecorView();
if (view == null) return;
InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null || !imm.isActive()) return;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Throwable e) {
e.printStackTrace();
}
}
Bu Çözüm, klavyeyi gizlediğinden emin olun, açılmazsa hiçbir şey yapmayın. Uzantı kullanır, böylece herhangi bir Bağlam Sahibi sınıfından kullanılabilir.
fun Context.dismissKeyboard() {
val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
val height = windowHeightMethod.invoke(imm) as Int
if (height > 0) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
}
Görünüm bağlamını kullanarak Kotlin'de aşağıdaki genişletme yöntemleri ile istenen sonucu elde edebiliriz:
/**
* Get the [InputMethodManager] using some [Context].
*/
fun Context.getInputMethodManager(): InputMethodManager {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getSystemService(InputMethodManager::class.java)
}
return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}
/**
* Dismiss soft input (keyboard) from the window using a [View] context.
*/
fun View.dismissKeyboard() = context
.getInputMethodManager()
.hideSoftInputFromWindow(
windowToken
, 0
)
Bunlar yerleştirildikten sonra şu numarayı arayın:
editTextFoo.dismiss()