Reputation: 83
I have an AlertDialog and I put EditText in it but i want my EditText to accept only the following input:
(0,1,2,3,4,5,6,7,8,9,.,,)
How can I achieve this?
Here is my code:
final AlertDialog.Builder alert = new AlertDialog.Builder(Treatment.this);
alert.setTitle("Weigthed Mean!");
alert.setIcon(R.drawable.droid);
Bundle b = getIntent().getExtras();
final TextView sum = (TextView) findViewById(R.id.fakeDB);
sum.setText(b.getCharSequence("input"));
final EditText x = new EditText (Treatment.this);
final EditText y = new EditText (Treatment.this);
x.setText(sum.getText().toString());
y.setHint("Enter the weights to the x values");
LinearLayout ll=new LinearLayout(Treatment.this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(x);
ll.addView(y);
alert.setView(ll);
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.setOnCancelListener(null);
}
});
alert.create();
alert.show();
Upvotes: 3
Views: 120
Reputation: 13483
final EditText x = new EditText (Treatment.this);
final EditText y = new EditText (Treatment.this);
x.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));
y.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));
Upvotes: 0
Reputation: 1426
@Osama is on the right track here, and you can read more about changing the type of keyboard here. It is also possible to specify which characters you would like to have appear in your EditText
by setting a KeyListener
:
x.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
x.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));
or something similar.
Upvotes: 2
Reputation: 7745
to set the EditText
type progromatically try:
x.setInputType(InputType.TYPE_CLASS_NUMBER);
you can use class with a flag to create decimal like:
x.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
but it wont add ',' to it however what @CodeMonkey is correct
check Input Types for more information about types of flags and classes
Upvotes: 3