簡體   English   中英

對話框內沒有用於EditText的軟鍵盤

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

我想創建一個對話框,用戶可以在其中插入數字。 所以我這樣重寫了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();
    }

但是盡管如此,我仍然被允許插入非數字字符,並且沒有軟鍵盤的信號……我的代碼有什么問題?

我不確定您可以通過編程方式執行什么操作,但是您可以創建自己的自定義布局,其中包含如下的edittext:

<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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM