0

我的小费计算器仅在我填写了两个文本字段时才起作用,我想让它显示一条消息,然后阻止应用程序强制关闭...

这是我尝试过的java,但它不起作用

  package tip.calculator;


import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.Button;




public class TipCalculator extends Activity 
{

private Button enter;
EditText myEditField ;
EditText myEditField2;
float percentage = 0;
float percentageInp = 0;
float billAmount = 0;
double output = 0; 
String output1 = "";
Button clearButton ;
TextView textView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myEditField = (EditText) findViewById(R.id.percentText);
    enter = (Button)findViewById(R.id.button1);
    myEditField2 = (EditText) findViewById(R.id.billText);
    clearButton = (Button) findViewById(R.id.clearButton);


    enter.setOnClickListener(new OnClickListener() {


        public void onClick(View v) {

             TextView errors;
             textView = (TextView) findViewById(R.id.textView1);
             errors = (TextView) findViewById(R.id.errorText);    



             if((myEditField2 != null) && (!TextUtils.isEmpty(myEditField2.getText().toString()))){


                percentageInp = Float.parseFloat(myEditField.getText().toString());
                billAmount = Float.parseFloat(myEditField2.getText().toString());

                percentage = ((float)percentageInp /100);

                output = (double)(billAmount * percentage);

                double result = output * 100;
                result = Math.round(result);
                result = result / 100;

                output1 = Double.toString(result);

                textView.setText(output1 + " $");

             } else{
                //Toast.makeToast(.....).show(); //Inform user about the error
             }

        }
    });

    clearButton.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            percentage = 0;
            output = 0;
            output1 = "";

            TextView textView = (TextView)findViewById(R.id.errorText);
            textView.setText("");

            TextView textView2 = (TextView)findViewById(R.id.percentText);
            textView2.setText("");

            TextView textView3 = (TextView)findViewById(R.id.billText);
            textView3.setText("");


            TextView textView1 = (TextView)findViewById(R.id.textView1);
            textView1.setText("");


            percentageInp = 0;
            billAmount = 0;

            myEditField.clearComposingText();
            myEditField2.clearComposingText();

        }


    });
}
}

这是堆栈跟踪,更新的java在上面

08-01 21:24:17.824: E/AndroidRuntime(19302): FATAL EXCEPTION: main
08-01 21:24:17.824: E/AndroidRuntime(19302): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
08-01 21:24:17.824: E/AndroidRuntime(19302):    at tip.calculator.TipCalculator$1.onClick(TipCalculator.java:48)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at android.view.View.performClick(View.java:3620)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at android.view.View$PerformClick.run(View.java:14292)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at android.os.Handler.handleCallback(Handler.java:605)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at android.os.Looper.loop(Looper.java:137)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at android.app.ActivityThread.main(ActivityThread.java:4507)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-01 21:24:17.824: E/AndroidRuntime(19302):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

检查文本字段是否为空不是问题。但是您绝对必须检查文本是否有空字符串和 null 。android.text.TextUtis.isEmpty() http://developer.android.com/reference/android/text/TextUtils.html#isEmpty(java.lang.CharSequence ) 在这种情况下很方便。此外,您必须确保输入的字符串是浮点数。因此,您还必须尝试捕获任何解析错误并将文本字段的 android:inputType 属性设置为 numberDecimal 以确保输入是浮点数。

因此:

  1. 在您的布局中,添加属性android:inputType="numberDecimal" ,以确保您的用户只能输入有效的十进制数。
  2. 做所有的安全检查
    if((myEditField2 != null) && (!TextUtils.isEmpty(myEditField2.getText().toString()))){
        //在这里使用你的数据
    } 别的{
       Toast.makeToast(.....).show() //通知用户错误
    }

如果你做 1,你可能不需要 Float.parseFloat(myEditField.getText().toString()) 用 try-catch来包围NumberFormatException

于 2012-08-02T02:56:15.973 回答
0

如果要检查编辑文本中的字符串是否为空,可以使用:

if(et.getText().toString() != null){

}
于 2012-08-02T02:49:23.837 回答