键盘在 Fragments 中隐藏 EditText
Keyboard hiding EditTexts in Fragments
编辑:
我需要使用键盘,但它隐藏了我的 EditText
,我需要它滚动以便键盘不会隐藏它。
我使用的是三星平板电脑。
我的风格:
parent="android:Theme.Holo.NoActionBar.Fullscreen"
EditText
字段在可滚动视图中,如下所示:
片段布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:text="@string/depot_code"/>
<EditText
android:hint="@string/enter_depot_code"
android:id="@+id/etlocationId"
android:imeOptions="actionNext"
android:inputType="number"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:lines="1"
android:maxLength="12"
android:singleLine="true">
<requestFocus/>
</EditText>
<TextView
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:text="@string/name"/>
<EditText
android:hint="@string/enter_name"
android:id="@+id/etname"
android:imeOptions="actionNext"
android:inputType="textPersonName"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:lines="1"
android:maxLength="24"
android:singleLine="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:text="@string/street"/>
<EditText
android:hint="@string/enter_street"
android:id="@+id/etstreet"
android:imeOptions="actionNext"
android:inputType="textPostalAddress"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:lines="1"
android:maxLength="24"
android:singleLine="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:text="@string/suburb"/>
<EditText
android:hint="@string/enter_suburb"
android:id="@+id/etsuburb"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:lines="1"
android:maxLength="24"
android:singleLine="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:text="@string/state"/>
<Spinner
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/spinner"
android:imeOptions="actionNext"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:spinnerMode="dropdown"/>
<TextView
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:text="@string/phone"/>
<EditText
android:hint="@string/enter_phone"
android:id="@+id/etphone"
android:imeOptions="actionDone"
android:inputType="phone"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:lines="1"
android:maxLength="12"
android:singleLine="true"/>
<Button
android:id="@+id/btnadd"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:text="@string/add"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
Activity布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tech_controller"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
< .../ Layouts. ../>
<FrameLayout
android:id="@+id/second"
android:layout_height="match_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
有人问过类似的问题,但我没有找到有效的答案。
我有一个项目使用许多附加到 activity 的片段。 在我的 activity 清单中,我有:
android:windowSoftInputMode="stateHidden"
部分片段需要用户输入。
在我使用的片段布局中:
每个片段嵌套如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent" .../>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<EditText
android:imeOptions="actionNext"
or
android:imeOptions="actionDone" .../>
这很好用。除了 键盘隐藏了位于页面下方的 EditText。
我一遍又一遍地读了这个:
http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions
尝试了这些建议,但是当 activity 被调用时键盘是可见的,这是我不想要的。它也不起作用。可能因为是片段?
SoftKeyboard hiding EditText
Android Keyboard hides EditText
我一直在尝试制定一个动态解决方案。感谢您的帮助。
编辑:这是在平板电脑上使用的,只有一个键盘选项,有语言变化。
编辑:我的解决方案
我已经通过安装另一个软键盘解决了这个问题。 https://code.google.com/p/softkeyboard/wiki/HowTo
尝试在 SO answer?
中提到的片段的onActivityCreated()
函数中调用 InputMethodManager.hideSoftInputFromWindow()
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AppUtil.hideKeyboard(getActivity(), getView());
}
其中 hideKeyboard()
看起来像这样
public static void hideKeyboard(Activity activity, View viewToHide) {
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(viewToHide.getWindowToken(), 0);
}
您可以尝试将其放入片段的 onActivityCreated 中:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
这不应该在加载或启动带有片段的 activity 时创建键盘。
将其更改为线性布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
到
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
(和结束标记)。注意方向。
更新清单删除这个:
<application ... >
<activity
android:windowSoftInputMode="stateVisible|adjustResize|adjustPan">
...
</activity>
...
</application>
改变这个(身高):
<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent">
我通过 google Play 商店安装另一个软键盘解决了这个问题。
正是 Petey's 评论让我探索了这个解决方案。
The IME you used has it's own "floating" layout, can you try your code with built-in IMEs like Google Keyboard or set your IME to a normal full-width layout?
所以我想,再多的视图布局也不会影响具有独立布局的软键盘。
我用过这个linkhttps://code.google.com/p/softkeyboard/wiki/HowTo
它甚至适用于 Android 清单:
android:windowSoftInputMode="stateHidden"
然后我使用 imeoptions
来控制编辑器光标在视图中的流动。
编辑更新
我刚刚在非品牌 android 平板电脑上试用了该应用程序,它配备了通用 android 键盘并且可以正常工作,无需更改任何东西。看来三星默认键盘很蛋疼
尝试仅将 WindowsSoftInputMode
用作 adjustpan
。
例如:
android:windowSoftInputMode="adjustPan"
对我有用的是在我的 FrameLayout 中以与您相同的方式使用 ScrollView,我在其中添加了此 属性:android:windowSoftInputMode="adjustPan"。最后,它看起来像这样并且正在运行。我不需要以编程方式更改任何内容:
<FrameLayout 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"
tools:context=".ProductFragment"
android:windowSoftInputMode="adjustPan">
<ScrollView
android:layout_height="wrap_content"
android:layout_width="match_parent">
...
</ScrollView>
</FrameLayout>
我已经尝试过此代码并解决了我的问题。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
请记住,隐藏键盘不适用于 getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
在显示您的 Snackbar 小部件之前,像这样隐藏键盘,
public void showSnackBar(){
InputMethodManager manager = (InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
manager.hideSoftInputFromWindow(getView().getWindowToken(),0);
Snackbar snackbar = Snackbar.make(getView(), "Some message!", Snackbar.LENGTH_INDEFINITE).show();
}