gpt4 book ai didi

android - 隐藏安卓虚拟键盘

转载 作者:行者123 更新时间:2023-11-29 16:10:38 25 4
gpt4 key购买 nike

我有一个显示 EditText 和两个底部的 Activity 。

当我点击 EditText 时,Android 虚拟键盘会出现,以便我可以输入文本。现在,在点击任何底部之前,我想隐藏键盘。我想通过点击屏幕来完成。

我在 stackoverflow 中看到过一些类似问题的帖子,但这看起来并不奏效。我试图设置一个监听器:

   // Create an anonymous implementation of OnFocusChangeListener
private OnFocusChangeListener mFocusListener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean b) {
// do something when the focus changes
hideSoftKeyboard(v);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setupUI(findViewById(R.id.parent));
EditText editText = (EditText) findViewById (R.id.edit_message);
editText.setOnFocusChangeListener(mFocusListener);
setContentView(R.layout.activity_main);
}

我还尝试创建一个父 Activity ,它递归地将一个 onTouch 事件关联到每个不是 TextView 的 View ,但它确实只注册了 TextView (我从另一个 stackoverflow 帖子中获取了这段代码)

    public void setupUI(View view) {

//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {

view.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(v);
return false;
}

});
}

//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {

for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

View innerView = ((ViewGroup) view).getChildAt(i);

setupUI(innerView);
}
}
}

对此有任何直接的解决方案吗?我不敢相信没有更简单的方法来做到这一点。我正在使用 Gingerbread API(API 级别 10)

谢谢

最佳答案

好的,我找到了一种非常简单的方法来做到这一点:XML 布局定义。由于 Layout 是一个 ViewGroup,我们可以在其上实现事件。去定义处理点击布局的方法(hideSoftKeyboard)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/main_layout"
android:onClick="hideSoftKeyboard" >

下面是我如何实现该方法:

public void hideSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}

关于android - 隐藏安卓虚拟键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13693880/

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