android号码对话框问题

时间:2016-03-11 07:00:51

标签: android dialog numbers

我可以使用数字(0,1,2,3,..,9)&清除,删除。但我不能使用小数。然后,当我删除时,我不想删除所有 .setText(""),我想在用户使用4个号码时逐个删除( 1234)。

    edt_cargo_info_money_amount = (EditText) contentView.findViewById(R.id.edt_cargo_info_money_amount);
    Button[] numbers = new Button[11];
    numbers[0] = (Button) contentView.findViewById(R.id.one);
    numbers[1] = (Button) contentView.findViewById(R.id.two);
    numbers[2] = (Button) contentView.findViewById(R.id.three);
    numbers[3] = (Button) contentView.findViewById(R.id.four);
    numbers[4] = (Button) contentView.findViewById(R.id.five);
    numbers[5] = (Button) contentView.findViewById(R.id.six);
    numbers[6] = (Button) contentView.findViewById(R.id.seven);
    numbers[7] = (Button) contentView.findViewById(R.id.eight);
    numbers[8] = (Button) contentView.findViewById(R.id.nine);
    numbers[9] = (Button) contentView.findViewById(R.id.zero);
    numbers[10] = (Button) contentView.findViewById(R.id.decimal);
    for (int i = 0; i < numbers.length; i++) {
        numbers[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_cargo_info_money_amount.append(((Button) v).getText().toString());
            }
        });
    }
    del = (Button) contentView.findViewById(R.id.del);
    del.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            edt_cargo_info_money_amount.setText(" ");
        }
    });
    success = (Button) contentView.findViewById(R.id.ok);
    cancel = (Button) contentView.findViewById(R.id.cancel);
    success.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Amount = edt_cargo_info_money_amount.getText().length() > 0 ? edt_cargo_info_money_amount.getText().toString() : null;
            txt_display_money.setText(edt_cargo_info_money_amount.getText());
            dialog.dismiss();
        }
    });
    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chk_cargo_info_money.setChecked(false);
            txt_display_money.setText("0.0");
            dialog.dismiss();
        }
    });

1 个答案:

答案 0 :(得分:2)

String text = edt_cargo_info_money_amount.getText().toString();
edit_cargo_info_money_amount.setText(text.substring(0, text.length()-1);

substr(0,length-1)删除最后一个字符,然后我们将编辑文本设置为该新字符串。