我想创建一个用户可以插入数字的对话框;所以我用这种方式覆盖了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();
}
但是尽管如此我仍然允许插入非数字字符,并且没有软键盘的迹象......我的代码出了什么问题?
答案 0 :(得分:0)
我不确定你能以编程方式做什么,但是你可以创建自己的自定义布局,其中包含一个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);