【问题标题】:No soft keyboard for the EditText inside the Dialog Box对话框内的 EditText 没有软键盘
【发布时间】:2011-11-09 18:53:38
【问题描述】:

我想创建一个对话框,用户可以在其中插入数字;所以我以这种方式覆盖了 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();
    }

但尽管如此,我仍然可以插入非数字字符,并且没有软键盘的迹象...我的代码有什么问题?

【问题讨论】:

    标签: android android-edittext android-softkeyboard dialog


    【解决方案1】:

    我不确定您可以通过编程方式做什么,但您可以创建自己的自定义布局,其中包含如下编辑文本:

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

    然后在 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);
    

    【讨论】:

    • 具体有哪些异常以及它们发生在哪些行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多