Reputation: 165
I am showing a dialog with edit text and set the input type decimal like this
**final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);**
It's input type working fine if I am using default alert.setPositiveButton and setNegativeButton click listeners. But when I used the
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { }
then it's input not working. Below is my code complete code
package com.Dialogs;
public class GetSalesTaxPopUp {
Context context;
private AlertDialog dialog;
public void getSalesTax(Context context2){
this.context = context2;
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Sales Tax");
alert.setMessage("Enter Tax");
// Set an EditText view to get user input
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
dialog = alert.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String salestax = input.getText().toString();
if(!salestax.equals("")){
Toast.makeText(context, salestax, Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "Enter Sales Tax", Toast.LENGTH_SHORT).show();
}
}
});
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!MyPreferences.getMyPreference("GetSalesTax", context).equals("")){
dialog.dismiss();
}
else
Toast.makeText(context, "Enter Sales Tax First", Toast.LENGTH_SHORT).show();
}
});
// alert.show();
}
}
Please guide me where I am doing something wrong. Any help is appreciated.
Upvotes: 1
Views: 1677
Reputation: 152927
You need both the input type class and the decimal flag:
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Upvotes: 4
Reputation: 68187
I think you need to call dialog.show();
at the very last.
Other than that, i would suggest you to use:
alert.setPositiveButton
instead of dialog.getButton(AlertDialog.BUTTON_POSITIVE)
Upvotes: 0