有没有办法只解析非空的EditTexts?

时间:2015-02-21 18:07:31

标签: android

我正在创建一个应用程序,可以解决倾斜三角形的缺失部分,如果它是可解的,但我很难解析来自EditTexts的数据。以下是我的代码示例:

Double A, B, C, a, b, c;
    aa = (EditText)findViewById(R.id.angleA);
    ab = (EditText)findViewById(R.id.angleB);
    ac = (EditText)findViewById(R.id.angleC);
    sa = (EditText)findViewById(R.id.sideA);
    sb = (EditText)findViewById(R.id.sideB);
    sc = (EditText)findViewById(R.id.sideC);

if (!aa.getText().toString().isEmpty()){
        A = Double.parseDouble(aa.getText().toString());
    }

    if (!ab.getText().toString().isEmpty()){
        B = Double.parseDouble(ab.getText().toString());
    }

    if (!ac.getText().toString().isEmpty()){
        C = Double.parseDouble(ac.getText().toString());
    }

    if (!sa.getText().toString().isEmpty()){
        a = Double.parseDouble(sa.getText().toString());
    }

    if (!sb.getText().toString().isEmpty()){
        b = Double.parseDouble(sb.getText().toString());
    }

    if (!sc.getText().toString().isEmpty()){
        c = Double.parseDouble(sc.getText().toString());
    }

我想知道是否有更短的编码方式。

3 个答案:

答案 0 :(得分:0)

你可以编写一个函数来完成工作:

Double A, B, C, a, b, c;

public void calc()
{
    A = getValueOrNan(R.id.angleA);
    B = getValueOrNan(R.id.angleB);
    C = getValueOrNan(R.id.angleC);
    a = getValueOrNan(R.id.sideA);
    b = getValueOrNan(R.id.sideA);
    c = getValueOrNan(R.id.sideA);

    if (A == Double.NaN)
    {
        // calc A
    }
    if (B == Double.NaN)
    {
        // calc B
    }

    // ...
}

public Double getValueOrNan(int id)
{
    EditText et = (EditText)findViewById(id);

    if (!et.getText().toString().isEmpty())
    {
        return Double.parseDouble(et.getText().toString());
    }
    else
    {
        return Double.NaN;
    }
}

答案 1 :(得分:0)

我能想到的最短,最易读的方式:

// Store EditText ids in an array
int[] editTextIds = new int[] { R.id.angleA, R.id.angleB,
        R.id.angleC, R.id.sideA, R.id.sideB, R.id.sideC };
// Store EditText values in an ArrayList
ArrayList<Double> results = new ArrayList<>();

// Loop ids, saving values to ArrayList
for (int id : editTextIds) {
    String value = ((EditText) findViewById(id)).getText().toString();
    if (TextUtils.isEmpty(value)) {
        results.add(null);
    } else {
        results.add(Double.valueOf(value));
    }
}

// Now:
// results.get(0) = A
// results.get(1) = B
// results.get(2) = C
// results.get(3) = a
// results.get(4) = b
// results.get(5) = c
// If results.get(x) returns null, means value isn't set

答案 2 :(得分:0)

使用TextUtils.isEmpty()和Java异常处理

aa = (EditText)findViewById(R.id.angleA);
ab = (EditText)findViewById(R.id.angleB);
ac = (EditText)findViewById(R.id.angleC);
sa = (EditText)findViewById(R.id.sideA);
sb = (EditText)findViewById(R.id.sideB);
sc = (EditText)findViewById(R.id.sideC);

//Rest of the stuff
//
Double saDouble=getVal(sa);


public Double getVal(EditText et){
Double op=Double.NAN;     
try{
if(!TextUtils.isEmpty(et.getText()))
   Double.parseDouble(et.getText().toString());
}catch(NumberFormatException e){
  //log,toast,dialog whatever you need to display.
}
 return op;
}