Android 可编辑微调器加虚拟键盘 - 这可能吗?

发布于 2024-12-03 09:16:45 字数 1660 浏览 6 评论 0原文

我有 AlertDialog 和可编辑的 Spinner,它允许我编辑选定的值,但有一个问题。我的 Spinner 内的编辑控件在点击后接受焦点,如果设备有物理键盘,一切都很好。但如果没有键盘...

虚拟键盘永远不会出现在该对话框中。即使我在对话框显示后强制键盘出现(不打开微调器列表),它也只能在后台出现并且无法使用。

是否可以有这样一个带有微调器的对话框并使用标准虚拟键盘在其编辑字段中写入?如何?

我的代码是这样的:

final AlertDialog.Builder alertb = new AlertDialog.Builder( this );
alertb.setPositiveButton( "OK", new DialogInterface.OnClickListener() ...
alertb.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() ...

final AlertDialog alert = alertb.create();
final Spinner inputSpinner = new Spinner(this);
ArrayList<String> names = new ArrayList<String>();
//... (filling 'names' here) ...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.editforspinner, names);
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
inputSpinner.setAdapter( adapter );
inputSpinner.setSelection( 0 );
alert.setView( inputSpinner, 4, 4, 4, 4 );
alert.show();

带有“editforspinner”的xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

我用这样的代码强制键盘:

inputSpinner.postDelayed( new Runnable() {
    @Override
    public void run() {
        InputMethodManager imm = (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE );
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
    } }, 2000 );

I have AlertDialog with an editable Spinner which allows me to edit the selected value, but there is a problem. The edit control inside my Spinner accepts the focus after tap and if the device has a physical keyboard all is fine. But if no keyboard...

The virtual keyboard never shows up with this dialog. Even if I force the keyboard to come up after dialog shows (without opening the spinner list), it comes in the background only and it is unusable.

Is it possible to have such a dialog with spinner and to write in its edit field with the standard virtual keyboard? How?

My code is something like this:

final AlertDialog.Builder alertb = new AlertDialog.Builder( this );
alertb.setPositiveButton( "OK", new DialogInterface.OnClickListener() ...
alertb.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() ...

final AlertDialog alert = alertb.create();
final Spinner inputSpinner = new Spinner(this);
ArrayList<String> names = new ArrayList<String>();
//... (filling 'names' here) ...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.editforspinner, names);
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
inputSpinner.setAdapter( adapter );
inputSpinner.setSelection( 0 );
alert.setView( inputSpinner, 4, 4, 4, 4 );
alert.show();

The xml file with 'editforspinner' is:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

I force the keyboard with such code:

inputSpinner.postDelayed( new Runnable() {
    @Override
    public void run() {
        InputMethodManager imm = (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE );
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS);
    } }, 2000 );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文