简体   繁体   English

对话框内没有用于EditText的软键盘

[英]No soft keyboard for the EditText inside the Dialog Box

I'd like to create a Dialog Box where the user can insert a number; 我想创建一个对话框,用户可以在其中插入数字。 so I overrided the onCreateDialog method this way: 所以我这样重写了onCreateDialog方法:

protected Dialog onCreateDialog(int id) {
            EditText editNum=new EditText(this);
            editNum.setMaxLines(1);
            editNum.setRawInputType(InputType.TYPE_CLASS_NUMBER);
            String strVal=listViewNum.getItemAtPosition(selectedItem).toString();
            strVal=strVal.substring(strVal.indexOf("=")+1,strVal.length()-1);
            editNum.setText(Util.formatNumber(Double.parseDouble(strVal)));
            editNum.selectAll();
            editNum.setGravity(android.view.Gravity.RIGHT);
            return new AlertDialog.Builder(this)
            .setTitle(getString(R.string.DialogEditNumberTitle))
            .setView(editNum)
            .setPositiveButton("OK", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                    }
            })
            .setNegativeButton("Annulla", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub

                    }
            })
            .create();
    }

But despite this I'm still allowed to insert non-numeric characters and there is no sign of the soft keyboard...what's wrong with my code? 但是尽管如此,我仍然被允许插入非数字字符,并且没有软键盘的信号……我的代码有什么问题?

I'm not sure what you could do programmatically, BUT you could create your own custom layout which will contain an edittext as follows: 我不确定您可以通过编程方式执行什么操作,但是您可以创建自己的自定义布局,其中包含如下的edittext:

<EditText android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inputType="numbers"
android:digits="0123456789"/>

and then in the code for the onCreateDialog: 然后在onCreateDialog的代码中:

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View myLayout = inflater.inflate(R.layout.myLayout, null); //I showed this as a linear layout before, but it must be a view to inflate it
        EditText myEditText = (EditText)myLayout.findViewById(R.id.myEditText);
        //create myEditText listener here, if needed




dialog.setView(myLayout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM