我有一个FragmentActivity
使用ViewPager
服务几个片段。每个都是ListFragment
,具有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<EditText android:id="@+id/entertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
启动活动时,软键盘会显示。为了解决这个问题,我在片段中做了以下内容:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Save the container view so we can access the window token
viewContainer = container;
//get the input method manager service
imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
. . .
}
@Override
public void onStart() {
super.onStart();
//Hide the soft keyboard
imm.hideSoftInputFromWindow(viewContainer.getWindowToken(), 0);
}
我从ViewGroup container
保存传入的onCreateView
参数,作为访问主要活动的窗口令牌的方法。这样运行没有错误,但键盘不会在hideSoftInputFromWindow
中调用onStart
时隐藏。
最初,我尝试使用膨胀的布局而不是container
,即:
imm.hideSoftInputFromWindow(myInflatedLayout.getWindowToken(), 0);
但是这扔了一个NullPointerException
,大概是因为片段本身不是一个活动而且没有一个独特的窗口令牌?
有没有办法在片段中隐藏软键盘,或者我应该在FragmentActivity
中创建一个方法并从片段中调用它?
只要您的Fragment创建了一个View,就可以在连接后使用该视图中的IBinder(窗口标记)。例如,您可以覆盖片段中的onActivityCreated:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
只需在代码中添加以下行:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
在Kotlin:
(activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(view?.windowToken,0)
除了以下代码行之外,我只能为我工作:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
如果将以下属性添加到活动的清单定义中,则会在活动打开时完全禁止键盘弹出。希望这有助于:
(添加到Activity的清单定义中):
android:windowSoftInputMode="stateHidden"
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container,
false);
someClass.onCreate(rootView);
return rootView;
}
在我的班级中保留我的根视图的实例
View view;
public void onCreate(View rootView) {
view = rootView;
使用视图隐藏键盘
public void removePhoneKeypad() {
InputMethodManager inputManager = (InputMethodManager) view
.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = view.getWindowToken();
inputManager.hideSoftInputFromWindow(binder,
InputMethodManager.HIDE_NOT_ALWAYS);
}
但DialogFragment
的例外情况,必须隐藏嵌入式Dialog
的焦点,而不是嵌入式EditText
中的第一个Dialog
this.getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
在片段中使用此代码
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
在我的情况下,当我从一个片段切换到另一个片段时,这将适用于我的情况
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
try {
InputMethodManager mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mImm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
mImm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
Log.e(TAG, "setUserVisibleHint: ", e);
}
}
}
从您喜欢的任何地方(活动/片段)使用此静态方法。
public static void hideKeyboard(Activity activity) {
try{
InputMethodManager inputManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
View currentFocusedView = activity.getCurrentFocus();
if (currentFocusedView != null) {
inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}catch (Exception e){
e.printStackTrace();
}
}
如果你想使用片段,只需调用hideKeyboard(((Activity) getActivity()))
。
这在API27上没有任何作用。我不得不在布局的容器中添加它,对我来说它是ConstraintLayout:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:focusedByDefault="true">
//Your layout
</android.support.constraint.ConstraintLayout>