我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
当前回答
android:windowSoftInputMode="stateAlwaysVisible" - manifest文件中的>。
edittext.requestFocus ();-代码为>。
这将打开软键盘上的编辑文本有请求焦点作为活动出现。
其他回答
要强制显示软键盘,可以使用
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);
}
我用定时器。键盘可以显示编辑文本聚焦。
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();
以下代码摘自谷歌的4.1 SearchView源代码。这似乎也适用于较小版本的Android。
private Runnable mShowImeRunnable = new Runnable() {
public void run() {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(editText, 0);
}
}
};
private void setImeVisibility(final boolean visible) {
if (visible) {
post(mShowImeRunnable);
} else {
removeCallbacks(mShowImeRunnable);
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
}
此外,需要在创建控件/活动时添加以下代码。(在我的例子中,它是一个复合控件,而不是一个活动)。
this.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
setImeVisibility(hasFocus);
}
});
您还可以创建EditText的自定义扩展,该扩展在接收到焦点时知道打开软键盘。这就是我最后要做的。以下是对我有效的方法:
public class WellBehavedEditText extends EditText {
private InputMethodManager inputMethodManager;
private boolean showKeyboard = false;
public WellBehavedEditText(Context context) {
super(context);
this.initializeWellBehavedEditText(context);
}
public WellBehavedEditText(Context context, AttributeSet attributes) {
super(context, attributes);
this.initializeWellBehavedEditText(context);
}
public WellBehavedEditText(Context context, AttributeSet attributes, int defStyleAttr) {
super(context, attributes, defStyleAttr);
this.initializeWellBehavedEditText(context);
}
public WellBehavedEditText(Context context, AttributeSet attributes, int defStyleAttr, int defStyleRes) {
super(context, attributes, defStyleAttr, defStyleRes);
this.initializeWellBehavedEditText(context);
}
private void initializeWellBehavedEditText(Context context) {
this.inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
final WellBehavedEditText editText = this;
this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if(showKeyboard) {
showKeyboard = !(inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_FORCED));
}
}
});
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(!focused) this.showKeyboard = false;
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
boolean result = super.requestFocus(direction, previouslyFocusedRect);
this.showKeyboard = true;
final WellBehavedEditText self = this;
this.post(new Runnable() {
@Override
public void run() {
showKeyboard = !(inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_FORCED));
}
});
return result;
}
}
根据这个答案,我使用了setSoftInputMode方法,并覆盖了DialogFragment中的这些方法:
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
requireDialog().getWindow().setSoftInputMode(InputMethodManager.HIDE_IMPLICIT_ONLY);
}
@Override
public void onStart() {
super.onStart();
requireDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
@Override
public void onStop() {
super.onStop();
requireDialog().getWindow().setSoftInputMode(InputMethodManager.HIDE_IMPLICIT_ONLY);
}
我也用这些方法创建了我自己的DialogFragment子类,所以当你创建另一个对话框并从这个类继承时,你会自动显示软键盘,而不需要任何其他编辑。希望对别人有用。