我的TextView没有显示我设置它的字符串。带有.getText参数的普通setText方法完全正常,但我与WorkWithChem类交互的部分却没有。
例如,如果我输入H2O作为我的化合物并以5摩尔输入,则moles的textview将显示5,但是gram textview会变为看似空字符串的内容。
我的猜测是它与onClick方法,xml文件或WorkWithChem类中的if语句有关。
这就是我所拥有的。
对于草率的代码提前抱歉,我对此比较新(比java更多的android开发)
AndroidManifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.justin.firstapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ActivityTwo">
android:label="@String/ActivityTwo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
活动java文件
package com.example.justin.firstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.math.BigDecimal;
import java.util.ArrayList;
public class ActivityTwo extends AppCompatActivity {
TextView textViewGrams, textViewMoles;
EditText editTextCompound, editTextGrams, editTextMoles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_two);
textViewGrams = (TextView) findViewById(R.id.textViewGrams);
textViewMoles = (TextView) findViewById(R.id.textViewMoles);
editTextCompound = (EditText) findViewById(R.id.editComp);
editTextGrams = (EditText) findViewById(R.id.editGrams);
editTextMoles = (EditText) findViewById(R.id.editMoles);
Button buttonCalculate = (Button) findViewById(R.id.buttonCalc);
buttonCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!editTextCompound.getText().equals(null)) {
ArrayList<Elements> comp = new WorkWithChem().compound(editTextCompound.getText().toString());
if (!editTextGrams.getText().equals(null) &&
!editTextMoles.getText().equals(null)) {
textViewMoles.setText(editTextMoles.getText());
textViewGrams.setText(editTextGrams.getText());
} else if (!editTextMoles.getText().equals(null)) {
textViewMoles.setText(editTextMoles.getText());
textViewGrams.setText(WorkWithChem.getGrams(comp, new BigDecimal(WorkWithChem.stringToNumber(editTextGrams.getText().toString()).doubleValue())).toString());
} else if (!editTextGrams.getText().equals(null)) {
textViewGrams.setText(editTextGrams.getText());
textViewMoles.setText(WorkWithChem.getMoles(comp, new BigDecimal(WorkWithChem.stringToNumber(editTextMoles.getText().toString()).doubleValue())).toString());
}
}
}
});
}
}
活动xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.justin.firstapp.ActivityTwo">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/untitled"
android:id="@+id/textViewMoles"
android:textAlignment="center"
android:textSize="25dp"
android:layout_above="@+id/textViewGrams"
android:layout_alignParentEnd="true"
android:layout_toEndOf="@+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Total moles"
android:id="@+id/textView2"
android:layout_alignBottom="@+id/textViewMoles"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/untitled"
android:id="@+id/textViewGrams"
android:textAlignment="center"
android:textSize="25dp"
android:layout_alignBottom="@+id/textView4"
android:layout_alignEnd="@+id/textViewMoles"
android:layout_toEndOf="@+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Total grams"
android:id="@+id/textView4"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editComp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="Enter a compound"
android:layout_alignEnd="@+id/textViewMoles"
android:textAlignment="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editGrams"
android:layout_below="@+id/editComp"
android:layout_alignParentStart="true"
android:hint="Enter grams" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editMoles"
android:layout_below="@+id/editComp"
android:layout_alignEnd="@+id/editComp"
android:hint="Enter moles" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="@+id/buttonCalc"
android:layout_below="@+id/editGrams"
android:layout_centerHorizontal="true"
android:onClick="thing"/>
</RelativeLayout>
WorkWithChem类
package com.example.justin.firstapp;
import java.math.*;
import java.util.*;
public class WorkWithChem{
public static boolean isCapitol(char c){
if(c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' || c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'J' || c == 'K' || c == 'L' || c == 'M' || c == 'N' || c == 'O' || c == 'P' || c == 'Q' || c == 'R' || c == 'S' || c == 'T' || c == 'U' || c == 'V' || c == 'W' || c == 'X' || c == 'Y' || c == 'Z')
return true;
return false;
}
public static ArrayList<Elements> compound (String str){
ArrayList<Elements> elements = new ArrayList();
String temp = str.toString();
for(int i = 1; i < str.length(); i++){
if(isCapitol(str.charAt(i))){
if(containsDigit(str)){
for(int addCounter = Integer.parseInt(extractNumber(str)); addCounter > 0; addCounter--){
elements.add(Elements.getBySymbol(str.substring(0, str.indexOf(extractNumber(str)))));
}
}else{
elements.add(Elements.getBySymbol(str.substring(0, i)));
}
temp = temp.substring(i);
}
}
elements.add(Elements.getBySymbol(temp));
return elements;
}
public static BigDecimal getGrams(BigDecimal moles, BigDecimal molarMass){
return moles.multiply(molarMass);
}
public static BigDecimal getGrams(ArrayList<Elements> compound, BigDecimal moles){
BigDecimal ans = new BigDecimal(0);
for(Elements e : compound){
BigDecimal temp = new BigDecimal(e.getAtomicMass().doubleValue());
ans.equals(ans.add(temp.multiply(moles)));
}
return ans;
}
public static BigDecimal getMolarMass(ArrayList<Elements> compound){
BigDecimal ans = new BigDecimal(0);
for(Elements e : compound){
ans.equals(ans.add(e.getAtomicMass()));
}
return ans;
}
public static final boolean containsDigit(String s) {
boolean containsDigit = false;
if (s != null && !s.isEmpty()) {
for (char c : s.toCharArray()) {
if (containsDigit = Character.isDigit(c)) {
break;
}
}
}
return containsDigit;
}
public static BigDecimal stringToNumber(String str){
String s = extractNumber(str);
return new BigDecimal(str);
}
public static String extractNumber(String str) {
if(str == null || str.isEmpty())
return "";
StringBuilder sb = new StringBuilder();
boolean found = false;
for(char c : str.toCharArray()){
if(Character.isDigit(c) || c == '.'){
sb.append(c);
found = true;
} else if(found){
// If we already found a digit before and this char is not a digit, stop looping
break;
}
}
return sb.toString();
}
public static BigDecimal getMoles(BigDecimal grams, BigDecimal molarMass){
return grams.multiply(molarMass);
}
public static BigDecimal getMoles(ArrayList<Elements> compound, BigDecimal molarMass){
BigDecimal ans = new BigDecimal(0);
for(Elements e : compound){
BigDecimal temp = new BigDecimal(e.getAtomicMass().doubleValue());
ans.equals(ans.add(temp.multiply(molarMass)));
}
return ans;
}
public static void main(String[] args){
ArrayList<Elements> list = compound("O");
BigDecimal mass = new BigDecimal(0);
BigDecimal moles = new BigDecimal(2);
for(int i = 0; i < list.size(); i++){
mass = mass.add(list.get(i).getAtomicMass());
}
System.out.println(getGrams(moles, mass));
for(Elements e : list){
System.out.println(e);
}
}
}
最后是元素枚举
package com.example.justin.firstapp;
import java.util.HashMap;
import java.util.Map;
import java.math.*;
public enum Elements {
/* SYMBOL NAME ATOMIC_WEIGHT ELECTRONEGATIVITY */
H("Hydrogen", 1.00794f, 2.1f), He("Helium", 4.0026f, 0), Li("Lithium",
6.941f, 0.98f), Be("Beryllium", 9.01218f, 1.57f), B("Boron",
10.811f, 2.04f), C("Carbon", 12.011f, 2.55f), N("Nitrogen",
14.0067f, 3.04f), O("Oxygen", 15.9994f, 3.44f), F("Fluorine",
18.9984f, 3.98f), Ne("Neon", 20.1797f, 0), Na("Sodium", 22.98977f,
0.93f), Mg("Magnesium", 24.305f, 1.31f), Al("Aluminum", 26.98154f,
1.61f), Si("Silicon", 28.0855f, 1.9f), P("Phosphorus", 30.97376f,
2.19f), S("Sulfur", 32.066f, 2.58f), Cl("Chlorine", 35.4527f, 3.16f), Ar(
"Argon", 39.948f, 0), K("Potassium", 39.0983f, 0.82f), Ca(
"Calcium", 40.078f, 1), Sc("Scandium", 44.9559f, 1.36f), Ti(
"Titanium", 47.88f, 1.54f), V("Vanadium", 50.9415f, 1.63f), Cr(
"Chromium", 51.996f, 1.66f), Mn("Manganese", 54.938f, 1.55f), Fe(
"Iron", 55.847f, 1.83f), Co("Cobalt", 58.9332f, 1.88f), Ni(
"Nickel", 58.6934f, 1.91f), Cu("Copper", 63.546f, 1.9f), Zn("Zinc",
65.39f, 1.65f), Ga("Gallium", 69.723f, 1.81f), Ge("Germanium",
72.61f, 2.01f), As("Arsenic", 74.9216f, 2.18f), Se("Selenium",
78.96f, 2.55f), Br("Bromine", 79.904f, 2.96f), Kr("Krypton", 83.8f,
0), Rb("Rubidium", 85.4678f, 0.82f), Sr("Strontium", 87.62f, 0.95f), Y(
"Yttrium", 88.9059f, 1.22f), Zr("Zirconium", 91.224f, 1.33f), Nb(
"Niobium", 92.9064f, 1.6f), Mo("Molybdenum", 95.94f, 2.16f), Tc(
"Technetium", 98, 1.9f), Ru("Ruthenium", 101.07f, 2.2f), Rh(
"Rhodium", 102.9055f, 2.28f), Pd("Palladium", 106.42f, 2.2f), Ag(
"Silver", 107.868f, 1.93f), Cd("Cadmium", 112.41f, 1.69f), In(
"Indium", 114.82f, 1.78f), Sn("Tin", 118.71f, 1.96f), Sb(
"Antimony", 121.757f, 2.05f), Te("Tellurium", 127.6f, 2.1f), I(
"Iodine", 126.9045f, 2.66f), Xe("Xenon", 131.29f, 2.6f), Cs(
"Cesium", 132.9054f, 0.79f), Ba("Barium", 137.33f, 0.89f), La(
"Lanthanum", 138.9055f, 1.1f), Ce("Cerium", 140.12f, 1.12f), Pr(
"Praseodymium", 140.9077f, 1.13f), Nd("Neodymium", 144.24f, 1.14f), Pm(
"Promethium", 145, 1.13f), Sm("Samarium", 150.36f, 1.17f), Eu(
"Europium", 151.965f, 1.2f), Gd("Gadolinium", 157.25f, 1.2f), Tb(
"Terbium", 158.9253f, 1.1f), Dy("Dysprosium", 162.5f, 1.22f), Ho(
"Holmium", 164.9303f, 1.23f), Er("Erbium", 167.26f, 1.24f), Tm(
"Thulium", 168.9342f, 1.25f), Yb("Ytterbium", 173.04f, 1.1f), Lu(
"Lutetium", 174.967f, 1.27f), Hf("Hafnium", 178.49f, 1.3f), Ta(
"Tantalum", 180.9479f, 1.5f), W("Tungsten", 183.85f, 2.36f), Re(
"Rhenium", 186.207f, 1.9f), Os("Osmium", 190.2f, 2.2f), Ir(
"Iridium", 192.22f, 2.2f), Pt("Platinum", 195.08f, 2.28f), Au(
"Gold", 196.9665f, 2.54f), Hg("Mercury", 200.59f, 2), Tl(
"Thallium", 204.383f, 2.04f), Pb("Lead", 207.2f, 2.33f), Bi(
"Bismuth", 208.9804f, 2.02f), Po("Polonium", 209, 2), At(
"Astatine", 210, 2.2f), Rn("Radon", 222, 0), Fr("Francium", 223,
0.7f), Ra("Radium", 226.0254f, 0.89f), Ac("Actinium", 227, 1.1f), Th(
"Thorium", 232.0381f, 1.3f), Pa("Protactinium", 231.0359f, 1.5f), U(
"Uranium", 238.029f, 1.38f), Np("Neptunium", 237.0482f, 1.36f), Pu(
"Plutonium", 244, 1.28f), Am("Americium", 243, 1.3f), Cm("Curium",
247, 1.3f), Bk("Berkelium", 247, 1.3f), Cf("Californium", 251, 1.3f), Es(
"Einsteinium", 252, 1.3f), Fm("Fermium", 257, 1.3f), Md(
"Mendelevium", 258, 1.3f), No("Nobelium", 259, 1.3f), Lr(
"Lawrencium", 262, 0), Rf("Rutherfordium", 261, 0), Db("Dubnium",
262, 0), Sg("Seaborgium", 263, 0), Bh("Bohrium", 262, 0), Hs(
"Hassium", 265, 0), Mt("Meitnerium", 266, 0), Uun("ununnilium",
269, 0), Uuu("unununium", 272, 0), Uub("ununbium", 277, 0);
static final class Holder {
/** Maps the atomic number to the Elements */
static final Map<Integer, Elements> map_atomicNumber = new HashMap<Integer, Elements>();
/** Maps the symbol to the Elements */
static final Map<String, Elements> map_symbol = new HashMap<String, Elements>();
}
private Elements(String fullName, float atomicMass, float electroNegativity) {
this.fullName = fullName;
this.atomicMass = new BigDecimal(atomicMass);
this.electroNegativity = new BigDecimal(electroNegativity);
Elements.Holder.map_atomicNumber.put(this.getAtomicNumber(), this);
Elements.Holder.map_symbol.put(this.name(), this);
}
private final String fullName;
private final BigDecimal atomicMass;
private final BigDecimal electroNegativity;
public int getAtomicNumber() {
return this.ordinal() + 1;
}
public String getSymbol() {
return this.name();
}
public String getFullName() {
return this.fullName;
}
public BigDecimal getAtomicMass() {
return this.atomicMass;
}
public BigDecimal getElectroNegativity() {
return this.electroNegativity;
}
public String toString() {
return name() + "[fullName=\"" + this.fullName + "\", atomicMass=\""
+ this.atomicMass + "\", electroNegativity=\""
+ this.electroNegativity + "\"]";
}
public static Elements getByAtomicNumber(int atomicNumber) {
return Elements.Holder.map_atomicNumber.get(atomicNumber);
}
public static Elements getBySymbol(String symbol) {
return Elements.Holder.map_symbol.get(symbol);
}
public static String getSymbolByAtomicNumber(int atomicNumber) {
Elements e = Elements.Holder.map_atomicNumber.get(atomicNumber);
if (e != null)
return e.getSymbol();
else
return null;
}
public static int getAtomicNumberBySymbol(String symbol) {
Elements e = Elements.Holder.map_atomicNumber.get(symbol);
if (e != null)
return e.getAtomicNumber();
else
return 0;
}
public static boolean exists(String symbol) {
if (null != Elements.Holder.map_symbol.get(symbol))
return true;
else
return false;
}
public static boolean exists(int atomicNumber) {
if (Elements.Holder.map_symbol.get(atomicNumber) != null)
return true;
else
return false;
}
public static String toCorrectSymbol(String symbol) {
String correct_symbol = "";
correct_symbol += Character.toUpperCase(symbol.charAt(0));
if (symbol.length() == 2) {
correct_symbol += Character.toLowerCase(symbol.charAt(1));
} else if (symbol.length() == 3) {
correct_symbol += Character.toLowerCase(symbol.charAt(1));
correct_symbol += Character.toLowerCase(symbol.charAt(2));
}
return correct_symbol;
}
}
答案 0 :(得分:0)
我相信空白的EditText上的getText()永远不会返回null - 它将返回一个空字符串。因此,首先检查两个字段是否都为非空始终正在执行,并且因为克是一个空字符串,所以这是放在TextView中的内容。尝试检查EditText的长度,而不是像
if (!(editTextGrams.getText().length() == 0) && !(editTextMoles.getText().length() == 0)) {
我认为这会解决您的问题。当从鼹鼠获得克数时也要注意你的计算,反之亦然,看起来你从鼹鼠得到克的情况是使用editTextGrams.getText()计算克数,但你已经确定该字段是空白的 - 在这种情况下你应该使用editTextMoles。基本上,我认为你只是在其他两个/如果情况下翻转变量。