رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
کاستومایز کردن EditText
#1
سلام شب بخیر،
برای وارد کردن قیمت در EditText (هنگام تایپ و آنی) سه رقم جدا کنه و کاما بزاره و بعد از عدد کلمه "تومان" اضافه بشه. دقیقا مثل دیوار => 300،000 تومان
بعد میخوام مقدار ورودی رو در دیتابیس بدون کاما و تومان ذخیره بشه ==> 300000
توی گوگل هم خیلی سرچ کردم آنچنان به نتیجه نرسیدم. لطفا اگر امکانش هست راهنمایی کنید.
پاسخ
تشکر شده توسط:
#2
اینو همینطوری عجله‌ای نوشتم:
public class Digit3EditText extends AppCompatEditText {
    public Digit3EditText(Context context) {
        super(context);
        init();
    }

    public Digit3EditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Digit3EditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            setText("0 تومان");
            addTextChangedListener(new TextWatcher() {
                private DecimalFormat formatter = new DecimalFormat("#");
                private String string;

                {
                    formatter.setGroupingUsed(true);
                    formatter.setGroupingSize(3);
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    string = s.toString().replaceAll("[^0-9]", "");
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int count, int before) {
                    String newText = s.toString().replaceAll("[^0-9]", "");
                    string += newText.length() > 0 ? newText.substring(0, 1) : "0";
                    try {
                        string = formatter.format(Long.parseLong(string)) + " تومان";
                    } catch (NumberFormatException e) {
                        string = formatter.format(Long.parseLong(string.substring(0, string.length() - 1))) + " تومان";
                    }
                    removeTextChangedListener(this);
                    setText(string);
                    addTextChangedListener(this);
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
    }
}
پاسخ
تشکر شده توسط: YN97
#3
سلام دستتون درد نکنه عالی بود و بدرستی کار میکنه اما وقتی میخوام یک عدد رو پاک کنم، پاک نمیشه و موقع ذخیره فقط عدد اول رو دخیره میکنه.
پاسخ
تشکر شده توسط:
#4
اون کد رو خیلی سریع نوشتم و تست نکرده بودم. این یکی تست شده. البته بجای getText باید از getPlainText استفاده کنید:
public class Digit3EditText extends AppCompatEditText {
    public Digit3EditText(Context context) {
        super(context);
        init();
    }

    public Digit3EditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Digit3EditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        if (!isInEditMode()) {
            setText("0 تومان");
            setSelection(getText().toString().length());
            setOnKeyListener(new OnKeyListener() {
                @Override
                public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                    if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
                        if (keyCode == KeyEvent.KEYCODE_DEL) {
                            backSpace();
                        } else {
                            DecimalFormat formatter = new DecimalFormat("#");
                            formatter.setGroupingUsed(true);
                            formatter.setGroupingSize(3);
                            String s = getText().toString().replaceAll("[^0-9]", "");
                            switch (keyCode) {
                                case KeyEvent.KEYCODE_0:
                                    s += "0";
                                    break;
                                case KeyEvent.KEYCODE_1:
                                    s += "1";
                                    break;
                                case KeyEvent.KEYCODE_2:
                                    s += "2";
                                    break;
                                case KeyEvent.KEYCODE_3:
                                    s += "3";
                                    break;
                                case KeyEvent.KEYCODE_4:
                                    s += "4";
                                    break;
                                case KeyEvent.KEYCODE_5:
                                    s += "5";
                                    break;
                                case KeyEvent.KEYCODE_6:
                                    s += "6";
                                    break;
                                case KeyEvent.KEYCODE_7:
                                    s += "7";
                                    break;
                                case KeyEvent.KEYCODE_8:
                                    s += "8";
                                    break;
                                case KeyEvent.KEYCODE_9:
                                    s += "9";
                                    break;
                            }
                            if (s.length() > 0) {
                                try {
                                    s = formatter.format(Long.parseLong(s)) + " تومان";
                                } catch (NumberFormatException e) {
                                    s = formatter.format(Long.parseLong(s.substring(0, s.length() - 1))) + " تومان";
                                }
                            } else {
                                s = "0 تومان";
                            }
                            setText(s);
                            setSelection(s.length());
                        }
                    }
                    return true;
                }
            });
            addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    if (after == 0) {
                        removeTextChangedListener(this);
                        backSpace();
                        addTextChangedListener(this);
                    }
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                }
            });
        }
    }

    public String getPlainText() {
        return getText().toString().replaceAll("[^0-9]", "");
    }

    public void backSpace() {
        DecimalFormat formatter = new DecimalFormat("#");
        formatter.setGroupingUsed(true);
        formatter.setGroupingSize(3);
        String s = getText().toString().replaceAll("[^0-9]", "");
        s = s.length() > 0 ? s.substring(0, s.length() - 1) : "0";
        if (s.length() > 0) {
            try {
                s = formatter.format(Long.parseLong(s)) + " تومان";
            } catch (NumberFormatException e) {
                s = formatter.format(Long.parseLong(s.substring(0, s.length() - 1))) + " تومان";
            }
        } else {
            s = "0 تومان";
        }
        setText(s);
        setSelection(s.length());
    }
}
پاسخ
تشکر شده توسط:




کاربران در حال بازدید این موضوع: 1 مهمان