Java 带有 Text Watcher 的应用程序,未执行代码

Java App with Text Watcher, not executing code

我的主要 activity 没有按照我的预期进行。我想这样做,只要编辑文本中的任何一个发生更改,并且如果两者都有内容,它就会使用它们执行计算。当我使用该应用程序时,只有顶部字段与文本观察器一起使用,而且只有一半的时间。有人可以帮助提供正确方法的示例吗?

 package com.software.roux.diabcalc;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

        SharedPreferences mPrefs;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setTitle("Insulin Calculator");
            mPrefs=this.getSharedPreferences("settings", 0);
        }


        public void addclick(View a) throws InterruptedException {

            if (a.getId() == R.id.add1) {

                final EditText a1 = (EditText) findViewById(R.id.add1);
                final EditText a2 = (EditText) findViewById(R.id.add2);

                final double bolusdose = Double.parseDouble(mPrefs.getString("bolus", "0"));
                final double correctiondose = Double.parseDouble(mPrefs.getString("correction", "0"));
                final double targetlow = Double.parseDouble(mPrefs.getString("low", "0"));
                final double targethigh = Double.parseDouble(mPrefs.getString("high", "0"));
                final double correctto = Double.parseDouble(mPrefs.getString("corrset", "0"));

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

                        String currentlevelst = a1.getText().toString();
                        String carbseatenst = a2.getText().toString();

                        if (currentlevelst.length() >= 1) {
                            if (carbseatenst.length() >= 1) {

                                Double currentlevel = Double.parseDouble(a1.getText().toString());
                                Double carbseaten = Double.parseDouble(a2.getText().toString());

                                double firststep = 0;

                                if (currentlevel > targethigh) {
                                    firststep = ((currentlevel - correctto) / correctiondose);
                                } else if (currentlevel < targetlow) {
                                    firststep = ((currentlevel - targethigh) / correctiondose);
                                } else {
                                    firststep = 0;
                                }

                                double secondstep = carbseaten / bolusdose;

                                double firstplussecond = firststep + secondstep;

                                double result = 0;

                                result = firstplussecond;

                                TextView t = (TextView) findViewById(R.id.answerobj);
                                t.setText("You Need To Use: " + result + " Units");

                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });


            }
        }



        public void addclickb(View b) throws InterruptedException {

            if (b.getId() == R.id.add2) {

                final EditText a1 = (EditText) findViewById(R.id.add1);
                final EditText a2 = (EditText) findViewById(R.id.add2);

                final double bolusdose = Double.parseDouble(mPrefs.getString("bolus", "0"));
                final double correctiondose = Double.parseDouble(mPrefs.getString("correction", "0"));
                final double targetlow = Double.parseDouble(mPrefs.getString("low", "0"));
                final double targethigh = Double.parseDouble(mPrefs.getString("high", "0"));
                final double correctto = Double.parseDouble(mPrefs.getString("corrset", "0"));

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

                        String currentlevelst = a1.getText().toString();
                        String carbseatenst = a2.getText().toString();

                        if (currentlevelst.length() >= 1) {
                            if (carbseatenst.length() >= 1) {

                                Double currentlevel = Double.parseDouble(a1.getText().toString());
                                Double carbseaten = Double.parseDouble(a2.getText().toString());

                                double firststep = 0;

                                if (currentlevel > targethigh) {
                                    firststep = ((currentlevel - correctto) / correctiondose);
                                } else if (currentlevel < targetlow) {
                                    firststep = ((currentlevel - targethigh) / correctiondose);
                                } else {
                                    firststep = 0;
                                }

                                double secondstep = carbseaten / bolusdose;

                                double firstplussecond = firststep + secondstep;

                                double result = 0;

                                result = firstplussecond;

                                TextView t = (TextView) findViewById(R.id.answerobj);
                                t.setText("You Need To Use: " + result + " Units");

                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });






            }
        }



        public void switchclick(View b){
            if(b.getId() == R.id.settings)
            {
                Intent myIntent = new Intent(MainActivity.this, Settings.class);
                MainActivity.this.startActivity(myIntent);
            }
        }
    }






<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="40px"
    android:paddingRight="40px"
    android:paddingTop="40px"
    tools:context="com.software.roux.diabcalc.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:text="Enter Your Information"
            android:textAlignment="center"
            android:textSize="30sp"
            tools:layout_editor_absoluteX="103dp"
            tools:layout_editor_absoluteY="52dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Blood Sugar Level" />

            <EditText
                android:id="@+id/add1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="number"
                android:onClick="addclick"
                tools:layout_editor_absoluteX="85dp"
                tools:layout_editor_absoluteY="114dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="72dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Grams Of Carbs Eaten" />

            <EditText
                android:id="@+id/add2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="number"
                android:onClick="addclickb"
                tools:layout_editor_absoluteX="85dp"
                tools:layout_editor_absoluteY="179dp" />
        </LinearLayout>

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="addclick"
            android:text="Calculate Needed Dose"
            tools:layout_editor_absoluteX="148dp"
            tools:layout_editor_absoluteY="240dp" />

        <TextView
            android:id="@+id/answerobj"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:textAlignment="center"
            android:textSize="24sp"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="241dp" />

        <Button
            android:id="@+id/settings"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom|center"
            android:onClick="switchclick"
            android:text="Settings" />

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Editext

不需要 onClick() 函数

仅使用 TextWatcher 即可完成工作

试试这个:

public class MainActivity extends AppCompatActivity {

SharedPreferences mPrefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final EditText a1 = (EditText) findViewById(R.id.add1);
    final EditText a2 = (EditText) findViewById(R.id.add2);

       a1.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) {
                        //Your codes
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //same for a2

    setTitle("Insulin Calculator");
    mPrefs=this.getSharedPreferences("settings", 0);
}

此外,我建议您将代码从 onTextChanged() 移至 afterTextChanged()

AfterTextChanged 将在您完成输入 Editext

时调用