我想在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);
}
});
的问候。
在Activity的onResume()部分,你可以调用方法bringKeyboard();
onResume() {
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
bringKeyboard(yourEditText);
}
protected boolean bringKeyboard(EditText view) {
if (view == null) {
return false;
}
try {
// Depending if edittext has some pre-filled values you can decide whether to bring up soft keyboard or not
String value = view.getText().toString();
if (value == null) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
return true;
}
} catch (Exception e) {
Log.e(TAG, "decideFocus. Exception", e);
}
return false;
}
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);
}
}
在onCrete() activity()方法的editText上调用requestFocus()方法,并在相同的editText上调用clearFocus()方法,当在键盘上单击完成。
要强制显示软键盘,可以使用
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
为了移除EditText上的焦点,你需要一个虚拟视图来抓取焦点。
关闭它,你可以使用
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
这适用于在对话框中使用它
public void showKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public void closeKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}