我对Android程序很陌生,所以我只是制作了一个简单的应用程序。
在这个应用程序中,我有一个EditText组件,当键盘弹出时,它会滑动,我不希望它向上滑动,所以我搜索了它,并开始使用它。
OnCreate法
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);但是问题是在使用了这一行之后,EditText已经就位了,点击enter后,键盘没有转到away.So,我搜索它,得到了隐藏键盘的方法
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);但现在的问题是键盘隐藏得很成功,但在我点击EditText.So之后就看不见了,我在网上搜索,但没有运气,于是开始在Android中搜索方法,使键盘可见,并找出了一些内容。
public static void showSoftKeyboard(Activity activity){
InputMethodManager inputMethodManager1 =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager1.showSoftInputFromInputMethod(
activity.getCurrentFocus().getWindowToken(), 0);但这是不起作用的是扔NullPointerException。所以有人能帮我吗。
此外,它是否有任何选择,以保持EditText在其立场,而不滑动上升。这样就不需要使用这个隐藏和显示方法了,如果没有,请告诉我如何把键盘带回来。
发布于 2018-08-03 06:33:23
评论你的逻辑,请尝试下面的方法,
在Android Manifest中的活动标记中写下一行,如下所示
<activity android:name=".yourActivityName"
android:windowSoftInputMode="adjustPan">
</activity>然后将这一行添加到edittext xml中的layout中。
android:imeOptions="actionDone"就像下面
<EditText
android:id="@+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edittext"
android:singleLine="true"
android:maxLines="1"
android:imeOptions="actionDone"/>或者根据您的代码设置
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE); 在软键盘上点击“完成”按钮,它将自动关闭。
下面是软键盘做button的click事件代码。
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
// write your code here
}
return false;
}
});发布于 2018-08-03 07:41:36
尝尝这个
public void showSoftKeyboard(Context context, View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}或
public void showSoftKeyboard(Context context){
if(context == null) return;
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}https://stackoverflow.com/questions/51666248
复制相似问题