首页 > 解决方案 > 否则如果在 textWatcher

问题描述

我是android编程的初学者,我正在开发我的第一个应用程序,我有4个editText盒子,我需要在第一个editText盒子“气流”和第二个盒子“frictionLoss”之间建立关系,然后结果必须存储在最后一个没有clickListner...的盒子然后我需要在第一个和第三个盒子“速度”之间建立关系,也是在第四个盒子中更新的结果,所以我做了一个textWatcher& 我添加了 else if 语句,主要问题是第一个 if 语句工作正常,但是每当我尝试更改第三个框的值时,它都不起作用,直到我从第二个框中手动删除应用程序中的条目,然后才会显示结果。


import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RectangularDuctActivity extends AppCompatActivity {
    public EditText airFlow;
    EditText frectionLoss;
    EditText velocity;
    TextView result;
    Button mbutton;
    EditText equivalentDia;
    EditText    recWidthMm;
    EditText    recHeightMm;
    int airFlowCfm = 100;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rectangular_duct);

        airFlow = findViewById(R.id.air_Flow_View);
        frectionLoss = findViewById(R.id.press_loss_View);
        equivalentDia = findViewById(R.id.equal_Dia_View);
        velocity = findViewById(R.id.velocity_View);
        result = findViewById(R.id.result_textview);
        mbutton = findViewById(R.id.cal_button);
        recWidthMm = findViewById(R.id.width_View);
        recHeightMm = findViewById(R.id.height_View);

        //text watcher is added to allow the cells to be updated once the related cells are edited
        // this watcher is controlling the Equivalent Dia & the velocity once Air flow & friction loss are editied
        TextWatcher textWatcher = 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) {
                double deFinal = 0;
                double a = Integer.parseInt(airFlow.getText().toString());
                if (!airFlow.getText().toString().equals("") && !frectionLoss.getText().toString().equals("")) {
                    //double a = Integer.parseInt(airFlow.getText().toString());
                    double b = Double.parseDouble(frectionLoss.getText().toString());
                    double de = (Math.pow(((0.109136 * Math.pow((a * 2.119), 1.9)) / (b * 12 / 98.1)), (1 / 5.02)) * 25.4);
                    deFinal = deFinal + de;
                    equivalentDia.setText(String.valueOf(deFinal));
                    velocity.setText(String.format("%.2f", (((a / 1000) / (Math.PI * (Math.pow((de / 2000), 2)))))));

                } else if (!airFlow.getText().toString().equals("") && !velocity.getText().toString().equals("")) {
                    //double a1 = Integer.parseInt(airFlow.getText().toString());
                    double b1 = Double.parseDouble(velocity.getText().toString());
                    double de2 = (a*10);
                    deFinal = deFinal + de2;
                    equivalentDia.setText(String.valueOf(deFinal));
                    //equivalentDia.setText(String.valueOf(Math.round(deFinal)));
                    //frectionLoss.setText(String.valueOf(Math.round(b1)));

                }else {
                    equivalentDia.getText().clear();
                    //velocity.getText().clear();
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        frectionLoss.addTextChangedListener(textWatcher);
        airFlow.addTextChangedListener(textWatcher);'''

标签: android

解决方案


昨天休息后,我调整了textWatcher代码并根据以下代码完全删除了 else if 语句

public void onTextChanged(CharSequence s, int start, int before, int count) {
            double deFinal = 0;
            if (!airFlow.getText().toString().equals("") && !frectionLoss.getText().toString().equals("")) {
                double a = Integer.parseInt(airFlow.getText().toString());
                double b = Double.parseDouble(frectionLoss.getText().toString());
                double de = (Math.pow(((0.109136 * Math.pow((a * 2.119), 1.9)) / (b * 12 / 98.1)), (1 / 5.02)) * 25.4);
                deFinal = deFinal + de;
                equivalentDia.setText(String.valueOf(deFinal));
                velocity.setText(String.format("%.2f", (((a / 1000) / (Math.PI * (Math.pow((de / 2000), 2)))))));
            }else {
                equivalentDia.getText().clear();
                velocity.getText().clear();
                frectionLoss.getText().clear();
            }

        }

然后之后我setOnClickListener根据下面的代码添加了第三个按钮的触摸

velocity.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {
            double b1 = Double.parseDouble(velocity.getText().toString());
            double a1 = Integer.parseInt(airFlow.getText().toString());
            frectionLoss.getText().clear();
            if (!airFlow.getText().toString().equals("") && !velocity.getText().toString().equals("")) {
                double areaV = (a1/1000)/b1;
                double de2 = (Math.pow((areaV*4)/Math.PI,0.5)*1000);
                double deltaH = 0.14939257 * (b1 / (Math.pow((de2/1000),3.02)));
                velocity.setText(String.format("%.2f", b1));
                equivalentDia.setText(String.valueOf(de2));
                frectionLoss.setText(String.format("%.2f", deltaH));

            } else {
                equivalentDia.getText().clear();
            }

        }
   });

现在该应用程序正在按我的意愿运行


推荐阅读