我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
当前回答
上面给出的所有解决方案(OnFocusChangeListener中的InputMethodManager交互)。onFocusChange监听器附加到您的EditText工作良好,如果您在活动中有单个编辑。
在我的例子中,我有两个编辑。
private EditText tvX, tvY;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tvX.setOnFocusChangeListener(this);
tvY.setOnFocusChangeListener(this);
@Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(tvX.hasFocus() || tvY.hasFocus()) {
imm.showSoftInput(v, 0);
} else {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
};
我观察到onFocusChange被触发为tvX与hasFocus=true(键盘显示),但随后为tvY与hasFocus=true(键盘隐藏)。最后,看不到键盘了。
一般解决方案应该有正确的语句在if“显示键盘,如果EditText文本有焦点”
其他回答
我在各种不同的情况下都有同样的问题,我发现的解决方案在一些情况下工作,但在其他情况下不工作,所以这里是一个在我发现的大多数情况下工作的组合解决方案:
public static void showVirtualKeyboard(Context context, final View view) {
if (context != null) {
final InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
view.clearFocus();
if(view.isShown()) {
imm.showSoftInput(view, 0);
view.requestFocus();
} else {
view.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
view.post(new Runnable() {
@Override
public void run() {
view.requestFocus();
imm.showSoftInput(view, 0);
}
});
view.removeOnAttachStateChangeListener(this);
}
@Override
public void onViewDetachedFromWindow(View v) {
view.removeOnAttachStateChangeListener(this);
}
});
}
}
}
view.requestFocus()可能由于不同的原因而不能工作。所以键盘不会显示。
如果视图是不可聚焦的,那么它实际上不会聚焦 (isFocusable返回false),或者由于其他原因无法聚焦 条件(不可聚焦的触摸模式(isFocusableInTouchMode),而 设备处于触摸模式、不可见、未启用或没有 大小)。
我使用了这个解决方案:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
EditText searchView = findViewById(R.id.searchView);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
showSoftKeyboard(searchView);
}
}, 300);
}
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
InputMethodManager imm = (InputMethodManager)
view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
信不信由你,当我发现活动动画可以禁用软键盘时,我的软键盘问题得到了解决。当你用
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
and
overridePendingTransition(0, 0);
它可以隐藏软键盘,没有办法显示它。
如果EditText是在回收器或ListView和/或这有禁用状态使用下面的代码。
public static void showKeyboardByFocus(final View view)
{
view.requestFocus();
InputMethodManager keyboard = SystemMaster.getInputMethodManager();
keyboard.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
Runnable re = new Runnable()
{
@Override
public void run()
{
view.setEnabled(true);
view.requestFocus();
}
};
Handler h = new Handler(Looper.getMainLooper());
h.postDelayed(re, 360);
}
android:windowSoftInputMode="stateAlwaysVisible" - manifest文件中的>。
edittext.requestFocus ();-代码为>。
这将打开软键盘上的编辑文本有请求焦点作为活动出现。