Java 安卓:点击按钮后隐藏键盘
我需要在点击按钮后隐藏android键盘 我已经看到了很多这样做的例子,但是,它们似乎都使用了特定的editText对象 e、 gJava 安卓:点击按钮后隐藏键盘,java,android,keyboard,hide,Java,Android,Keyboard,Hide,我需要在点击按钮后隐藏android键盘 我已经看到了很多这样做的例子,但是,它们似乎都使用了特定的editText对象 e、 g 我的问题是,我正在动态构建屏幕,因此可能会有许多编辑文本字段。是否有一种方法可以隐藏键盘,而无需指定隐藏键盘的editText对象 您可以将其设置为您的布局,即: LinearLayout mainLayout; // Get your layout set up, this is just an example mainLayout = (LinearLayou
我的问题是,我正在动态构建屏幕,因此可能会有许多编辑文本字段。是否有一种方法可以隐藏键盘,而无需指定隐藏键盘的editText对象 您可以将其设置为您的布局,即:
LinearLayout mainLayout;
// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);
// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);
这是一个假设您的布局将被创建的示例,不管有多少EditText
对象(或其他对象)放置在布局上
编辑:另外,我发现非常有用的一点是确保在活动首次启动时隐藏键盘(即:如果EditText
是第一个关注的对象)。为此,我将其放入onCreate()
活动方法中:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
别忘了使用try catch blog,因为如果键盘未打开,并且使用键盘隐藏代码应用程序将崩溃
try {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
您可以使用以下代码隐藏键盘,可能是在按钮单击事件上:
//================ Hide Virtual Key Board When Clicking==================//
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
//======== Hide Virtual Keyboard =====================//
fun Fragment.hideKeyboard() {
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}
fun AppCompatActivity.hideKeyboard() {
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}
你使用这个代码
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
如果问题发生在某个活动上,则只需执行以下操作即可:
try {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
否则,如果片段中需要代码,请执行以下操作
try {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
// TODO: handle exception
}
这将处理在按钮点击时隐藏键盘或在事件块中写入时认为特定的任何其他事件。记录并基于@burmat和@Prashant Maheshwari Andro的回答
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
假设您调用单击按钮,如下所示。其中button和RoidLogin_button是button对象
protected void onCreate(Bundle savedInstanceState) {
// your code...
buttonAndroidLogin_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboard((Button)v);
// ....
} // end onCreate
在“活动”上,必须设置下一个方法
public void hideKeyboard(View view) {
try {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch(Exception ignored) {
}
}
它使用相同的按钮隐藏输入,因此我们不需要任何线性布局、文本视图或任何其他任意控件。此外,代码还可用于更多按钮。在KOTLIN中:
//================ Hide Virtual Key Board When Clicking==================//
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
//======== Hide Virtual Keyboard =====================//
fun Fragment.hideKeyboard() {
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}
fun AppCompatActivity.hideKeyboard() {
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}
在您的片段中
:
//================ Hide Virtual Key Board When Clicking==================//
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
//======== Hide Virtual Keyboard =====================//
fun Fragment.hideKeyboard() {
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}
fun AppCompatActivity.hideKeyboard() {
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}
创建如下扩展:
//================ Hide Virtual Key Board When Clicking==================//
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
//======== Hide Virtual Keyboard =====================//
fun Fragment.hideKeyboard() {
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}
fun AppCompatActivity.hideKeyboard() {
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}
然后像这样使用它:
hideKeyboard()
hideKeyboard()
在您的活动中
:
//================ Hide Virtual Key Board When Clicking==================//
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
//======== Hide Virtual Keyboard =====================//
fun Fragment.hideKeyboard() {
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}
fun AppCompatActivity.hideKeyboard() {
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}
创建如下扩展:
//================ Hide Virtual Key Board When Clicking==================//
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
//======== Hide Virtual Keyboard =====================//
fun Fragment.hideKeyboard() {
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}
fun AppCompatActivity.hideKeyboard() {
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}
然后像这样使用它:
hideKeyboard()
hideKeyboard()
您可以为整个活动隐藏它,这很好地总结了它:注意,它也适用于其他视图部分,例如EditText:),然后您可以更改以下行:
imm.hideSoftInputFromWindow(myEditText.getWindowToken(),0)代码>迄今为止最简单、最优雅的解决方案最优雅、最通用、最简单的解决方案,我喜欢它的精确答案