Thursday, 22 August 2013

Cannot set InputType number in AlertDialog with EditText

Cannot set InputType number in AlertDialog with EditText

I have an AlertDialog with 2 EditText and I need that second edit text
show only the number in the android soft keyboard
My try is this
OnClickListener addNewItemListener = new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(
MyActivity.this);
LinearLayout myLayout= new LinearLayout(MyActivity.this);
myLayout.setOrientation(LinearLayout.VERTICAL);
alert.setTitle(R.string.add_title);
alert.setMessage(R.string.add_message);
final TextView t1 = new TextView(MyActivity.this);
t1.setText("Name");
final EditText input1 = new EditText(MyActivity.this);
final TextView t2 = new TextView(MyActivity.this);
t2.setText("Value");
EditText input2 = new EditText(MyActivity.this);
input2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
final EditText input2f = input2;
myLayout.addView(t1);
myLayout.addView(input1);
myLayout.addView(t2);
myLayout.addView(input2f);
alert.setView(myLayout);
alert.setPositiveButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
alert.setNegativeButton(R.string.add,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
try {
String name =
input1.getText().toString();
double value =
Double.parseDouble(input2f
.getText().toString());
addItem(name, value);
} catch (RuntimeException e) {
Alerts.DatiErrati(MyActivity.this);
}
}
});
alert.show();
}
};
unfortunately also the second EditText input2f show the standard text
keyboard, ignoring
input2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
How could I fix this?

No comments:

Post a Comment