I'd like to create a Dialog Box where the user can insert a number; so I overrided the onCreateDialog method this way:
protected Dialog onCreateDialog(int id) {
EditText editNum=new EditText(this);
editNum.setMaxLines(1);
editNum.setRawInputType(InputType.TYPE_CL开发者_如何转开发ASS_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 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:
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);
精彩评论