gpt4 book ai didi

Android:在自定义对话框中触摸时隐藏键盘

转载 作者:搜寻专家 更新时间:2023-11-01 08:07:25 25 4
gpt4 key购买 nike

我有一个自定义的对话框。在对话框中,有一个 EditText。当我触摸 EditText 内部时,会显示软键盘。当我触摸对话框的其他地方时,我想隐藏这个键盘。

我知道如何在 Activity 中隐藏键盘。我只是不知道如何在 Dialog 中执行此操作。

谢谢。

最佳答案

您可以使用 focuslisner 轻松做到这一点,请参阅下面的代码示例:

EditText.setOnFocusChangeListener(new View.OnFocusChangeListener() 
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EditText, InputMethodManager.SHOW_IMPLICIT);
}
else
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0);
}
}
});

编辑:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable = "true"
android:isFocusableInTouchMode = "true"
android:clickable = "true" >

<EditText
android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="hint"
android:singleLine="true" />

</LinearLayout>
</RelativeLayout>

\确保获得焦点:

    LinearLayout actionHide = (LinearLayout) findViewById(R.id.wrapper);
actionHide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
actionHide.requestFocus(); // use this to trigger the focus listner
//or use code below to set the keyboard to hidden
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0);

}
});

关于Android:在自定义对话框中触摸时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13216670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com