Softkeyboard没有出现在AlertDialog的手机只出现在、手机、Softkeyboard、AlertDialog

2023-09-05 03:01:13 作者:卖萌的小行家

为什么softkeyboard是只显示在平板电脑上的一个谜!

Why the softkeyboard is showing only on the tablet is a mystery !

下面是我用code。

AlertDialog.Builder builder = new AlertDialog.Builder(CurrentActivityName.this);
builder.setTitle("Title");
builder.setMessage("Message");
final EditText input = new EditText(CurrentActivityName.this);
builder.setView(input);
builder.setPositiveButton(R.string.allow, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//my code
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//my code
}
});
builder.create().show();

我能够通过解决这个问题 使用 postDelayed 与毫秒数发表的的Runnable 的

I am able to solve it by using postDelayed with a number of milliseconds to post a Runnable

 input.requestFocus();
 input.postDelayed(new Runnable() {
 @Override
 public void run() {
 InputMethodManager keyboard = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
 keyboard.showSoftInput(input, 0);
                    }
                },200);

硬codeD延迟永远不会建议,因为他们可能会推出的未predictable行为的下的不同的条件/不同的设备的

Hard-coded delays are never recommended because they may introduce unpredictable behavior under different conditions / different devices.

我要寻找一些稳定的解决方案。

I am looking for some stable solution.

推荐答案

我解决了这个问题。

AlertDialog alertDlg = builder.create();

alertDlg.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

alertDlg.show();