Android 可编辑微调器加虚拟键盘 - 这可能吗?
我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论