如何在textchangelistner上将两个edittext的值相乘,并在android中显示结果在anthor edittext中,反之亦然

时间:2017-01-13 06:56:24

标签: android

我将两个edittext et_weight和et_rate中的值相乘,并在另一个edittext中得到结果。现在,我想在et_freight中更改值时,将et_freight和et_weight除以得到et_rate中的结果。任何人都建议使用方法

      et_weight.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) {

                    if (!et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
                        et_freight.setText(String.valueOf(Integer.valueOf(et_weight.getText().toString()) * Integer.valueOf(et_rate.getText().toString())));
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            et_rate.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) {

                    if (!et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
                        et_freight.setText(String.valueOf(Integer.valueOf(et_weight.getText().toString()) * Integer.valueOf(et_rate.getText().toString())));
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

    et_freight.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) {
            if (!et_freight.getText().toString().equals("") && !et_weight.getText().toString().equals("")) {
                et_rate.setText(String.valueOf(Integer.valueOf(et_freight.getText().toString()) / Integer.valueOf(et_weight.getText().toString())));
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

1 个答案:

答案 0 :(得分:0)

您可以通过在更改EditText之前检查if (et_weight.isFocused() && !et_rate.getText().toString().equals("") && !et_weight.getText().toString().equals("")) { 是否重点关注来实现此目的。例如第一个条件:

private void updateDivide(){
    int weight = Integer.valueOf(et_weight.getText().toString());
    int freight = Integer.valueOf(et_freight.getText().toString());
    et_rate.setText(String.valueOf(freight / weight));
}

private void updateMultiply(){
    int weight = Integer.valueOf(et_weight.getText().toString());
    int rate = Integer.valueOf(et_rate.getText().toString());
    et_freight.setText(String.valueOf(weight * rate));
}

private boolean isSet(EditText editText){
    return !editText.getText().toString().matches("");
}

此外,为了简化代码,您可以创建一些帮助方法,如下所示:

et_weight.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){

        if (et_weight.isFocused() && isSet(et_weight) && isSet(et_rate)) {
            updateMultiply();
        }

    }

    @Override
    public void afterTextChanged (Editable s){

    }
});

et_rate.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){

        if (et_rate.isFocused() && isSet(et_rate) && isSet(et_weight)) {
            updateMultiply();
        }

    }

    @Override
    public void afterTextChanged (Editable s){

    }
});

et_freight.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){

        if (et_freight.isFocused() && isSet(et_weight) && isSet(et_freight)) {
            updateDivide();
        }

    }

    @Override
    public void afterTextChanged (Editable s){

    }
});

然后在你的代码中使用它:

<?php
$render = '';
$filenames = '';
$dir = './upload';
$aFilenames = scandir($dir);

    if(isset($_FILES["file"]["error"]) > 1){
        echo "Error: ".$_FILES["file"]["error"]."<br />";

    } elseif (isset($_FILES["file"]["name"])) {
        $filename = basename($_FILES['file']['name']);
        move_uploaded_file($_FILES['file']['tmp_name'],'upload/'.$filename);

        $render .= "name file: " .$_FILES['file']['name'] .'<br>';
        $render .= "type file: " .$_FILES['file']['type'] .'<br>';
        $render .= "size: " .($_FILES['file']['size']/1024) .'kb <br>';
        $render .= "saved in: " .$_FILES['file']['tmp_name'] .'<br>';
        $render .= '<img src="upload/'.$_FILES['file']['name'].'" height="40px">';
    }
    else {
        echo 'Upload a file';
    }
foreach ($aFilenames as $key) {
        $filenames .= $key . '<br>';
}
var_dump($aFilenames);

?>