How to get a result data to edit text without clic

2019-09-14 22:24发布

问题:

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("");
    }
}

回答1:

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
            }
        });


回答2:

yes you can you by detecting keyboard action done

i.e.

et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // set result in second edit text 
            return true;
        }
        return false;
    }
});


回答3:

Here you can add TextWatcher for input EditText like this,

  input.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

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


                    // Check some Validations and Conditions here

                    // Convert function goes here if all validationand conditions checked
                    convert();

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

Hope it will help you my friend !