我的主要活动并没有按照我的意图去做。我想在任何一个编辑文本被更改时都这样做,如果两者都有内容,它将使用它们执行计算。当我使用应用程序时,顶部字段与文本观察者一起使用,只有一半时间。有人可以帮助提供一个正确的方法来做这个吗?
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>
答案 0 :(得分:0)
onClick()
Editext
函数
仅使用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