本文介绍了如何在不单击按钮的情况下获取结果数据来编辑文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个转换器应用程序,我使用了2个编辑文本来获取输入和其他显示输出,通过单击按钮可以显示输出.有什么方法可以在不使用按钮单击事件的情况下以2个编辑文本显示输出.当我们在1个编辑文本中输入内容时,我想自动将结果放入2个编辑文本中.

I created a converter app, I used 2 edit text one which gets input and other display output it will display output by clicking on button is there any way to get the output display in 2 edit text without using click event of button. I want to get result automatically placed in 2 edit text when we enter input in 1 edit text.

EditText input,result;

input = (EditText) findViewById(R.id.ip);
result = (EditText) findViewById(R.id.res);  // my edit texts input and result
result.setClickable(false);
public void convert(View view){ //when clicking it get result to 2 edit text, but I want to get automatically to the second edit text when user enter the input 
    if (!input.getText().toString().equals("")){
        ufrom = (String) sp1.getSelectedItem();
        uto = (String) sp2.getSelectedItem();
        Double ip = Double.valueOf(input.getText().toString());
        TemperatureConverter.Units fromUnit = TemperatureConverter.Units.fromString(ufrom);
        TemperatureConverter.Units toUnit = TemperatureConverter.Units.fromString(uto);
        double r = con.TemperatureConvert(fromUnit,toUnit,ip);
        result.setText(String.valueOf(r));
    } else {
        result.setText("");
    }
}

推荐答案

您可以为此目的使用TextWatcher.您将获得此侦听器中的每个字符值..根据需要更新输出editText

you can use TextWatcher for this purpose. You wil get the each character value in this listener.. Update your output editText according to your needs

editText1.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                // Place the logic here for your output edittext
            }
        });

这篇关于如何在不单击按钮的情况下获取结果数据来编辑文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 22:48