确保edittext不为空

时间:2017-12-16 21:01:19

标签: java android

我想确保在按下提交按钮以停止应用程序崩溃之前字段不为空。

这是我的代码



package com.alrabei.edcalactivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class edcalactivity extends AppCompatActivity {

    EditText firstNumber;
    EditText secondNumber;
    EditText thirdnum;
    EditText forthnum;
    TextView showresult;
    Button buadd1;


    double num1,num2,num3,num4,sum1,sum2,sum3,TotalBuy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edcal_activity);

        firstNumber = (EditText)findViewById(R.id.s1);
        secondNumber = (EditText)findViewById(R.id.p1);
        thirdnum = (EditText)findViewById(R.id.s2);
        forthnum = (EditText)findViewById(R.id.p2);
        showresult = (TextView)findViewById(R.id.result);
        buadd1 = (Button)findViewById(R.id.bu1);



        buadd1.setOnClickListener(new View.OnClickListener() {



          public void onClick(View v) {


              num1 = Double.parseDouble(firstNumber.getText().toString());
              num2 = Double.parseDouble(secondNumber.getText().toString());
              num3 = Double.parseDouble(thirdnum.getText().toString());
              num4 = Double.parseDouble(forthnum.getText().toString());





              if((firstNumber.getText().length()>0) && (secondNumber.getText().length()>0) && (thirdnum.getText().length()>0) && (forthnum.getText().length()>0))
              {
                  sum1 = num1 + num3;
                  TotalBuy = (num1 * num2) + (num3 * num4);
                  // sum2 = num2 + num4;
                  // sum3 = sum2 / sum1;
                  sum3 = TotalBuy / sum1;

                  showresult.setText(Double.toString(sum3));
              }
              else{
                  showresult.setText("Error");
              }



           }

        });



    }



}




这是xml文件



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context="com.alrabei.edcalactivity">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="العدد السابق" />

        <EditText
            android:id="@+id/s1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ems="10"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="السعر:" />

        <EditText
            android:id="@+id/p1"
            android:layout_width="171dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number|numberDecimal" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="الجديد" />

        <EditText
            android:id="@+id/s2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="القيمة:" />

        <EditText
            android:id="@+id/p2"
            android:layout_width="173dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number|numberDecimal" />

    </LinearLayout>

    <Button
        android:id="@+id/bu1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:maxLength="5"
        />

</LinearLayout>
&#13;
&#13;
&#13;

我做了一个if语句,以确保用户在edittext中输入一个数字,但它不起作用..你能帮我解决代码有什么问题吗?

2 个答案:

答案 0 :(得分:0)

解析数字后进行检查。这样Double.parseDouble()在检查空字符串之前就已经尝试访问空字符串了。

您需要围绕onClick()函数的整个内容包装if语句。

答案 1 :(得分:0)

首先检查EditView是否为空:

if(!firstNumber.getText().toString().trim().equals("")){
//To Do
    num1 = Double.parseDouble(firstNumber.getText().toString());
}