gpt4 book ai didi

android - 键盘解雇不适用于 Android

转载 作者:行者123 更新时间:2023-11-29 01:37:51 25 4
gpt4 key购买 nike

代码正在运行并打印出“dismissing keyboard”,但键盘并未关闭。 View v 只是按钮和文本字段在相关布局中位于顶部的背景 View 。我在 S3 API 18 上运行

public View v;
public EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.v = (View) findViewById(R.id.backgroundView);
this.et = (EditText) findViewById(R.id.searchBox);
Button testButton = (Button) findViewById(R.id.testButton);
testButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("dismissing keyboard");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(getWindow().getCurrentFocus().getWindowToken() ,0);
}
});
this.v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("dismissing keyboard");
//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromInputMethod(getWindow().getCurrentFocus().getWindowToken() ,0);
}
});

}





<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">


<View
android:id="@+id/backgroundView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"


/>

<EditText
android:id="@+id/searchBox"
android:layout_width="fill_parent"
android:layout_height= "wrap_content"
android:layout_centerVertical="true"

/>

<Button
android:id="@+id/searchButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/searchBox"
android:text="Search"
/>

<Button
android:id="@+id/testButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/searchBox"
android:text="testButton"
/>

最佳答案

您传递的 View 应该是编辑文本。

public static void hideKeyboard(Context context, View layoutThatContainsEditText) {
((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(layoutThatContainsEditText.getApplicationWindowToken(), 0);
}

关于android - 键盘解雇不适用于 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26774979/

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