Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 开发具有100'的应用程序;编辑字段和文本字段的类型_Android_Android Studio_Android Textwatcher - Fatal编程技术网

Android 开发具有100'的应用程序;编辑字段和文本字段的类型

Android 开发具有100'的应用程序;编辑字段和文本字段的类型,android,android-studio,android-textwatcher,Android,Android Studio,Android Textwatcher,我正在开发一个包含100个文本字段和编辑字段的应用程序。在很多文本中,我使用textwatcher来监听更改和更新其他文本字段。在这个问题上我没有太多的选择,因为许多领域相互依赖。但该应用程序遇到了很多性能问题。它经常结冰。我可以得到一些关于开发文本字段数量惊人的应用程序的提示吗。我认为我使用的所有TextWatcher都在减慢ui线程的速度 //Business Operating Cost TextWatcher businessOperatingCostTextWatc

我正在开发一个包含100个文本字段和编辑字段的应用程序。在很多文本中,我使用textwatcher来监听更改和更新其他文本字段。在这个问题上我没有太多的选择,因为许多领域相互依赖。但该应用程序遇到了很多性能问题。它经常结冰。我可以得到一些关于开发文本字段数量惊人的应用程序的提示吗。我认为我使用的所有TextWatcher都在减慢ui线程的速度

  //Business Operating Cost
        TextWatcher businessOperatingCostTextWatcher = new TextWatcher() {
            private double edtTaxPayment = 0.0;
            private double txtPaymentBusinessLoan = 0.0;
            private double edtOtherBusiness = 0.0;
            private double edtBasicServices = 0.0;
            private double edtTransport = 0.0;
            private double edtLocalRental = 0.0;
            private double edtSalaries = 0.0;
            private double unforeseen = 0.0;

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

            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mCashFlowBinding.edtSalaries != null && mCashFlowBinding.edtSalaries.length() != 0) {
                    edtSalaries = Double.parseDouble(mCashFlowBinding.edtSalaries.getText().toString());
                }
                if (mCashFlowBinding.edtLocalRental != null && mCashFlowBinding.edtLocalRental.length() != 0) {
                    edtLocalRental = Double.parseDouble(mCashFlowBinding.edtLocalRental.getText().toString());
                }
                if (mCashFlowBinding.edtTransport != null && mCashFlowBinding.edtTransport.length() != 0) {
                    edtTransport = Double.parseDouble(mCashFlowBinding.edtTransport.getText().toString());
                }
                if (mCashFlowBinding.edtBasicServices != null && mCashFlowBinding.edtBasicServices.length() != 0) {
                    edtBasicServices = Double.parseDouble(mCashFlowBinding.edtBasicServices.getText().toString());
                }
                if (mCashFlowBinding.edtOtherBusiness != null && mCashFlowBinding.edtOtherBusiness.length() != 0) {
                    edtOtherBusiness = Double.parseDouble(mCashFlowBinding.edtOtherBusiness.getText().toString());
                }
                if (mCashFlowBinding.txtPaymentBusinessLoan != null && mCashFlowBinding.txtPaymentBusinessLoan.length() != 0) {
                    txtPaymentBusinessLoan = Double.parseDouble(mCashFlowBinding.txtPaymentBusinessLoan.getText().toString());
                }
                if (mCashFlowBinding.edtTaxPayment != null && mCashFlowBinding.edtTaxPayment.length() != 0) {
                    edtTaxPayment = Double.parseDouble(mCashFlowBinding.edtTaxPayment.getText().toString());
                }
                if (mCashFlowBinding.txtUnforseen != null && mCashFlowBinding.txtUnforseen.length() != 0) {
                    unforeseen = Double.parseDouble(mCashFlowBinding.txtUnforseen.getText().toString());
                }
                double resultSum = (edtTaxPayment + txtPaymentBusinessLoan + edtOtherBusiness + edtBasicServices + edtTransport + edtLocalRental + edtSalaries + unforeseen);

                if (!Double.isNaN(resultSum) && !Double.isInfinite(resultSum)) {
                    mCashFlowBinding.txtBusinessOperatingCost1.setText(String.valueOf(resultSum));
                } else {
                    mCashFlowBinding.txtBusinessOperatingCost1.setText("0.0");
                }
            }
        };

        mCashFlowBinding.edtSalaries.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtLocalRental.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtTransport.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtBasicServices.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtOtherBusiness.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.txtPaymentBusinessLoan.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.edtTaxPayment.addTextChangedListener(businessOperatingCostTextWatcher);
        mCashFlowBinding.txtUnforseen.addTextChangedListener(businessOperatingCostTextWatcher);

如何简化主UI线程:

向视图公开ViewModel的一些LiveData,只需在TextWatcher中调用
postValue
。然后它会很快释放

然后,在LiveData observer中,您可以在单独的线程上处理处理,并将结果再次发送到LiveData对象
finalOperstingCosts

然后,在该LiveData的观察者中,将文本设置为字段


这样,主线程上就不会有长时间运行的任务。

一次只激活一个
TextWatcher
,因此这可能不是原因。检查主线程中是否有其他工作正在进行