Android AlertDialog中编辑文本的默认焦点和键盘
我正在使用Android中的AlertDialog.Builder快速提示用户输入文本。对话框显示出来,效果很好,但用户必须单击EditText字段才能加载软键盘。是否有任何方法可以在我的对话框打开时打开键盘并将焦点放在屏幕上?这是我的密码:Android AlertDialog中编辑文本的默认焦点和键盘,android,dialog,Android,Dialog,我正在使用Android中的AlertDialog.Builder快速提示用户输入文本。对话框显示出来,效果很好,但用户必须单击EditText字段才能加载软键盘。是否有任何方法可以在我的对话框打开时打开键盘并将焦点放在屏幕上?这是我的密码: final Map<String,Object> rowData = itemList.get(mPosition); final EditText input = new EditText(search
final Map<String,Object> rowData = itemList.get(mPosition);
final EditText input = new EditText(searchList.getContext());
input.requestFocus();
input.setSingleLine();
final AlertDialog dialog = new AlertDialog.Builder(searchList.getContext())
.setTitle(StringUtils.getSafeString(rowData.get("label")))
.setView(input)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
rowData.put("value", StringUtils.getSafeString(input.getText()));
searchList.invalidateViews();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).create();
dialog.show();
final Map rowData=itemList.get(mPosition);
最终EditText输入=新的EditText(searchList.getContext());
input.requestFocus();
input.setSingleLine();
final AlertDialog dialog=新建AlertDialog.Builder(searchList.getContext())
.setTitle(StringUtils.getSafeString(rowData.get(“标签”))
.setView(输入)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int whichButton){
rowData.put(“value”,StringUtils.getSafeString(input.getText());
searchList.invalidateViews();
}
}).setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
public void onClick(对话框接口对话框,int whichButton){
//什么也不做。
}
}).create();
dialog.show();
在XML布局中
召唤
在默认编辑文本中
<EditText
android:blabla
.... >
<requestFocus/>
</EditText>
使用以下代码。这对我有用
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager= (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.toggleSoftInput(InputMethodManager.SHOW\u强制,0);
隐藏键盘使用:
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(view.getWindowToken(),0)
或者尝试下面的代码,但必须将requestFocus()或设置为edittext
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
在Android对话框中以编程方式设置编辑文本焦点时隐藏键盘。 我也遇到了这个问题,这是一个非常简单的解决方案——下面是我建议的解决方案。虽然它对我有效,但我看不出为什么它对你的情况无效 基本上不会触发软键盘,因为视图是以编程方式创建的。实际的修复方法只是将这一行放在onCreateDialog方法中:
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
从Android文档中:
如果用户关注编辑文本,软键盘将
自动出现。为了迫使我们的
我们称之为程序性重点
getDialog().getWindow().setSoftInputMode()。请注意,许多窗口
您以前可能在对话框中执行的操作仍然可以
在DialogFragment中完成,但必须调用getDialog().getWindow()
而不仅仅是getWindow()
如果您更经常需要,请使用自定义视图
public class RequestFocusEditText extends AppCompatEditText {
private RequestFocusEditText self;
public RequestFocusEditText(final Context context, AttributeSet attrs) {
super(context, attrs);
this.self = this;
setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
post(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
requestFocus();
}
}
构建器不使用XML布局,它只接受在代码中创建的EditText。我尝试在多个地方调用生成的EditText上的RequestFocus,但它似乎没有按照我希望的方式工作。对于这个问题,这不是一个有效的解决方案。在dialog.setView中使用XML,再加上RightHandedMonkey的答案,这是唯一对我有效的方法。加油!这应该是公认的答案。我使用了
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u INPUT\u ADJUST\u PAN | WindowManager.LayoutParams.SOFT\u INPUT\u STATE\u VISIBLE)
在我的AppCompatDialog子类的构造函数中。dialog.getWindow()可能返回Null没有LayoutParams.SOFT\u INPUT\u STATE\u这样的东西可见,因此此代码无法编译。@Mitch这是关于此的文档:也许您缺少WindowManager的导入?在这两种情况下,当目标为>=P(API 9)时,这可能不再是必需的。我以前尝试过很多方法。。这是正确的。非常感谢,这段代码非常适合AlertDialogy,有时有效,有时无效。大约30%的显示对话框-键盘未显示:(关于有时对其他人不起作用的注释,如果您放置editText.postDelayed(可扩展,100),则其效果为100%;)当editText失去焦点时,这不会显示键盘。似乎不对
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//setup your dialog builder and inflate the view you want here
...
//Make sure your EditText has the focus when the dialog is displayed
edit.requestFocus();
//Create the dialog and save to a variable, so we can set the keyboard state
Dialog dialog = builder.create();
//now, set to show the keyboard automatically
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return dialog;
}
public class RequestFocusEditText extends AppCompatEditText {
private RequestFocusEditText self;
public RequestFocusEditText(final Context context, AttributeSet attrs) {
super(context, attrs);
this.self = this;
setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
post(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
requestFocus();
}
}