如何将编辑文本设置为输出和输入?

时间:2013-11-11 18:29:35

标签: android button android-edittext

我想知道如何使用多个 EditText ,检测其中一个是否被修改并计算设置未修改的 EditText 的txt的值与另一个 它应该像公式解算器:4 EditText 1 按钮

如果其中一个 EditText 为空,则按下按钮时,必须将空文本的文本设置为使用其他值获得的值。实际上它似乎不起作用,当我按下计算按钮时,活动会崩溃。

public class motolineare extends Activity implements OnClickListener {
    EditText et1;
    EditText et2;
    EditText et3;
    EditText et4;

    Button calculate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.formula);
        TextView textView1 = (TextView) findViewById(R.id.textView1);

        textView1.setGravity(Gravity.CENTER_HORIZONTAL);

        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        et3 = (EditText) findViewById(R.id.et3);
        et4 = (EditText) findViewById(R.id.et4);

        calculate = (Button) findViewById(R.id.calculate);

        calculate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        double num1 = 0;
        double num2 = 0;
        double num3 = 0;
        double num4 = 0;
        double res = 0;

        if ((TextUtils.isEmpty(et1.getText().toString()) & TextUtils
                .isEmpty(et2.getText().toString()))
                || ((TextUtils.isEmpty(et1.getText().toString()) & TextUtils
                        .isEmpty(et3.getText().toString())))
                || ((TextUtils.isEmpty(et1.getText().toString()) & TextUtils
                        .isEmpty(et4.getText().toString())))
                || ((TextUtils.isEmpty(et2.getText().toString()) & TextUtils
                        .isEmpty(et3.getText().toString())))
                || ((TextUtils.isEmpty(et2.getText().toString()) & TextUtils
                        .isEmpty(et4.getText().toString())))
                || ((TextUtils.isEmpty(et4.getText().toString()) & TextUtils
                        .isEmpty(et3.getText().toString())))) {
            return;
        }

        num1 = Double.parseDouble(et1.getText().toString());
        num2 = Double.parseDouble(et2.getText().toString());
        num3 = Double.parseDouble(et3.getText().toString());
        num4 = Double.parseDouble(et4.getText().toString());

        switch (v.getId()) {
        case R.id.calculate:
            if ((TextUtils.isEmpty(et1.getText().toString()))) {
                res = (num2 * num3) / num4;

                editText.setText(" " + res);
            } else if ((TextUtils.isEmpty(et2.getText().toString()))) {
                res = (num1 * num4) / num3;

                EditText editText = (EditText) findViewById(R.id.et2);
                editText.setText(" " + res);

            } else if ((TextUtils.isEmpty(et3.getText().toString()))) {
                res = (num4 * num1) / num2;

                EditText editText = (EditText) findViewById(R.id.et3);
                editText.setText(" " + res);
            } else if ((TextUtils.isEmpty(et4.getText().toString()))) {
                res = (num2 * num3) / num1;

                EditText editText = (EditText) findViewById(R.id.et4);
                editText.setText(" " + res);
            }
            break;
        default:
            break;
        }
    }
}

Logcat输出:

11-11 19:05:22.130: E/AndroidRuntime(1911): FATAL EXCEPTION: main
11-11 19:05:22.130: E/AndroidRuntime(1911): java.lang.NumberFormatException: Invalid double: ""
11-11 19:05:22.130: E/AndroidRuntime(1911):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at java.lang.StringToReal.parseDouble(StringToReal.java:248)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at java.lang.Double.parseDouble(Double.java:295)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at com.example.easyphysics.motolineare.onClick(motolineare.java:73)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at android.view.View.performClick(View.java:4084)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at android.view.View$PerformClick.run(View.java:16966)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at android.os.Handler.handleCallback(Handler.java:615)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at android.os.Looper.loop(Looper.java:137)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at java.lang.reflect.Method.invokeNative(Native Method)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at java.lang.reflect.Method.invoke(Method.java:511)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-11 19:05:22.130: E/AndroidRuntime(1911):     at dalvik.system.NativeStart.main(Native Method)
11-11 19:05:25.371: E/Trace(1932): error opening trace file: No such file or directory (2)

2 个答案:

答案 0 :(得分:0)

看起来像其中一个

  num1 = Double.parseDouble(et1.getText().toString());
  num2 = Double.parseDouble(et2.getText().toString());
  num3 = Double.parseDouble(et3.getText().toString());
  num4 = Double.parseDouble(et4.getText().toString());
当您尝试对其进行计算时,

为空。第73行中的任何一个都会导致问题..而不是所有这些条件,甚至是它们,可能更容易拥有try/catch。像

这样的东西
try
{
     res= (num2*num3)/num4;
}
catch (NumberFormatException e)
{
    // display a message saying that the box is empty
}

但你可以将整个事情包裹在try/catch

答案 1 :(得分:0)

来自EditText的输入为空或包含一些空格。 而不是

num1 = Double.parseDouble(et1.getText().toString());

您可以使用:

String s = etN.getText().toString().trim();
numN = s.isEmpty()? Double.NaN:Double.parseDouble(s);

NetN中的numN替换为1..4。您可以将Double.NaN替换为空字段的含义。