我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
当前回答
我用定时器。键盘可以显示编辑文本聚焦。
edittext = (EditText) findViewById(R.id.edittext );
edittext.requestFocus();
edittext.setFocusableInTouchMode(true);
if (edittext.requestFocus()) {
final Thread timer = new Thread() {
public void run() {
try{
sleep(500);
InputMethodManager imm =(InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imm.showSoftInput(edittext, SHOW_IMPLICIT);
} catch (Exception e) {
e.printStackTrace();
}
}
};
timer.start();
其他回答
我同意raukodraug的观点,在swithview中使用时,你必须像这样请求/清除焦点:
final ViewSwitcher viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
final View btn = viewSwitcher.findViewById(R.id.address_btn);
final View title = viewSwitcher.findViewById(R.id.address_value);
title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
viewSwitcher.showPrevious();
btn.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(btn, InputMethodManager.SHOW_IMPLICIT);
}
});
// EditText affiche le titre evenement click
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btn.clearFocus();
viewSwitcher.showNext();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(btn.getWindowToken(), 0);
// Enregistre l'adresse.
addAddress(view);
}
});
的问候。
我使用这个扩展函数来显示Kotlin的键盘。
fun EditText.requestFocusWithKeyboard() {
post {
dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0f, 0f, 0))
dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0f, 0f, 0))
setSelection(length())
}
}
你可以把它叫做:
editText.requestFocusWithKeyboard()
除了非常方便之外:
您不必担心在所有可能的情况下关闭它,例如当您使用InputMethodManager.SHOW_FORCED导航返回时。 除了请求焦点,它还设置选择(光标到文本末尾)。
Kotlin扩展显示键盘的焦点。
这是之前的回答的组合,要么太长,要么不完整。
这个扩展在消息队列上发布了一个可运行的消息,在请求聚焦后显示软键盘:
fun View.showSoftKeyboard() {
post {
if (this.requestFocus()) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
}
}
在需要时从任何视图调用它:
editText.showSoftKeyboard()
当我阅读官方文档时,我认为这是最好的答案,只是将视图传递给参数,如您的EditText,但showSoftKeyboard似乎不能在横向上工作
private fun showSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
private fun closeSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
没有一个答案对我有用。这里有一个简单的方法。
searchEditText.setVisibility(View.VISIBLE);
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
searchEditText.requestFocus();
}
}, 400);
只是将requestFocus()方法延迟了400ms。