简体   繁体   English

Android EditText 输入随着我们在上面打字而改变(数字)

[英]Android EditText input changing as we typing on it(number)

i'm making an app that can calculate payOnloan,prepaid,etc(3 months project from my supervisor, i'm having an intern).But i got stuck at some point.I want to have an EditText where it will automatically change its value, in this case numbers as we input the value.Example:我正在制作一个可以计算 payOnloan、预付等的应用程序(我的主管 3 个月的项目,我有一个实习生)。但我在某个时候卡住了。我想要一个 EditText,它会自动改变它的值,在本例中为我们输入值时的数字。示例:

whene type 1 in EditText output 1 when we type 10 in EditText output 10 when we type 100 in EditText output 100 when we type 1.000 in EditText output 1.000 when we type 10.000 in EditText output 10.000 and so on..(until 1.000.000.000.000) note:for one EditText,i mean we give the input on it and the output also on the same edittext当我们在 EditText 中输入 10 时,在 EditText 输出 1 中输入 1 当我们在 EditText 中输入 100 时输出 100 当我们在 EditText 中输入 1.000 时输出 1000 当我们在 EditText 输出 10.000 中输入 10.000 时输入 1..(直到 1.000.0.000) 000) 注意:对于一个 EditText,我的意思是我们在它上面给出输入,输出也在同一个 edittext 上

is it possible to do this?i've tried almost everything related like number separator or decimal format or currency convert.But none of that works.有可能做到这一点吗?我已经尝试了几乎所有相关的东西,比如数字分隔符或十进制格式或货币转换。但这些都不起作用。

i cant attach image file sry about that,我不能附上图像文件 sry,

thanks in advance,提前致谢,

Regards, George问候, 乔治

So if I understand you correctly, you have an Edit text box and whatever you type inside that it needs to be reflected on some other view?所以如果我理解正确的话,你有一个编辑文本框,你在里面输入的任何内容都需要反映在其他视图上? If that is the case, a text watcher is what you are looking for.如果是这种情况,则文本观察器就是您要寻找的。

Here is an example of text watcher:下面是一个文本观察器的例子:

final TextWatcher mTextEditorWatcher = new TextWatcher(){
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {


        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            yourView.setText(yourEditText.getText().toString()); 
        }

        public void afterTextChanged(Editable s) {

        }
    };

Pay attention to: public void onTextChanged However beware of null values!注意: public void onTextChanged但是要注意空值!

Check the length of your number in the text watcher on textChange and use the following to add decimals:在 textChange 上的文本观察器中检查号码的长度,并使用以下内容添加小数:

  DecimalFormat df = new DecimalFormat("#.00");
  String inputFormated = df.format(yourEditText.getText.toString());
  System.out.println(inputFormated);

How did you define your decimal Format?你是如何定义十进制格式的?

Try something like this:尝试这样的事情:

DecimalFormat formattedStuff = new DecimalFormat("#,###,###,###,##0");
String receivedStr = yourEditText.getText().toString();
yourEditText.setText(formattedStuff.format(receivedStr));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM