我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
当前回答
芬兰湾的科特林:
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
fun showKeyboard() {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
fun hideKeyboard() {
imm.hideSoftInputFromWindow(phoneNoInputTxt.windowToken, 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);
}
我在使用视图动画时也遇到了类似的问题。所以我已经放了一个动画监听器,以确保在尝试请求对所显示的编辑文本进行键盘访问之前,我将等待动画结束。
bottomUp.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if (textToFocus != null) {
// Position cursor at the end of the text
textToFocus.setSelection(textToFocus.getText().length());
// Show keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textToFocus, InputMethodManager.SHOW_IMPLICIT);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
代码片段……
public void hideKeyboard(Context activityContext){
InputMethodManager imm = (InputMethodManager)
activityContext.getSystemService(Context.INPUT_METHOD_SERVICE);
//android.R.id.content ( http://stackoverflow.com/a/12887919/2077479 )
View rootView = ((Activity) activityContext)
.findViewById(android.R.id.content).getRootView();
imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
}
public void showKeyboard(Context activityContext, final EditText editText){
final InputMethodManager imm = (InputMethodManager)
activityContext.getSystemService(Context.INPUT_METHOD_SERVICE);
if (!editText.hasFocus()) {
editText.requestFocus();
}
editText.post(new Runnable() {
@Override
public void run() {
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
});
}
你可以试着强迫软键盘出现,这对我来说是有效的:
...
dialog.show();
input.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
在onCrete() activity()方法的editText上调用requestFocus()方法,并在相同的editText上调用clearFocus()方法,当在键盘上单击完成。