当用户跳过其中一个edittext并在下一个edittext上输入数据时,如何提示错误?

时间:2016-08-10 18:14:06

标签: android

在此我有8个edittext,如果用户在edittext1输入输入,则跳过edittext 2并在edittext 3中输入数据。我需要向用户提示错误,并且try块内的else部分也不会被执行我错了怎么纠正这个逻辑错误

package com.sabari.results;


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 android.widget.Toast;


public class Calculator extends AppCompatActivity {

    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    TextView tv7;
    TextView tv8;
    TextView tv10;
    EditText et1;
    EditText et2;
    EditText et3;
    EditText et4;
    EditText et5;
    EditText et6;
    EditText et7;
    EditText et8;
    Button b1;
    Button b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("CGPA Calculator");
        setContentView(R.layout.calculator);

        tv1 = (TextView) findViewById(R.id.textView);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.textView5);
        tv6 = (TextView) findViewById(R.id.textView6);
        tv7 = (TextView) findViewById(R.id.textView7);
        tv8 = (TextView) findViewById(R.id.textView8);


        b1 = (Button) findViewById(R.id.button);
        b2 = (Button)findViewById(R.id.button2);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                et1 = (EditText) findViewById(R.id.editText);
                et2 = (EditText) findViewById(R.id.editText2);
                et3 = (EditText) findViewById(R.id.editText3);
                et4 = (EditText) findViewById(R.id.editText4);
                et5 = (EditText) findViewById(R.id.editText5);
                et6 = (EditText) findViewById(R.id.editText6);
                et7 = (EditText) findViewById(R.id.editText7);
                et8 = (EditText) findViewById(R.id.editText8);






                tv10 = (TextView) findViewById(R.id.textView10);
                int n=1;


                double no1,no2,no3,no4,no5,no6,no7,no8;
                double g1=0,g2=0,g3=0,g4=0,g5=0,g6=0,g7=0,g8=0;
                try {
                    no1 = Double.parseDouble(et1.getText().toString());
                    if(no1>=5&&no1<=10)
                    {
                        g1=no1;
                        n=1;
                    }
                    else{
                        Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
                    }
                }catch (NumberFormatException e){
                    e.printStackTrace();

                }
                try{
                    no2 = Double.parseDouble(et2.getText().toString());
                    if(no2>=5&&no2<=10)
                    {
                        g2=no2;
                        n=2;
                    }
                    else{
                        Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
                    }

                }
                catch (NumberFormatException e){
                      e.printStackTrace();
                }
                try {
                    no3 = Double.parseDouble(et3.getText().toString());
                    if(no3>=5&&no3<=10)
                    {
                        g3=no3;
                        n=3;
                    }
                    else{
                        Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
                    }

                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
                try {
                    no4 = Double.parseDouble(et4.getText().toString());
                    if(no4>=5&&no4<=10)
                    {
                        g4=no4;
                        n=4;
                    }
                    else{
                        Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
                    }

                }catch (NumberFormatException e)
                {
                    e.printStackTrace();
                }
                try{
                    no5 = Double.parseDouble(et5.getText().toString());
                    if(no5>=5&&no5<=10)
                    {
                        g5=no5;
                        n=5;
                    }
                    else{
                        Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
                    }
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
                try{
                    no6 = Double.parseDouble(et6.getText().toString());
                    if(no6>=5&&no6<=10)
                    {
                        g6=no6;
                        n=6;
                    }
                    else{
                        Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
                    }
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
                try {
                    no7 = Double.parseDouble(et7.getText().toString());
                    if(no7>=5&&no7<=10)
                    {
                        g7=no7;
                        n=7;
                    }
                    else{
                        Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
                    }
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }try {
                    no8 = Double.parseDouble(et8.getText().toString());
                    if(no8>=5&&no8<=10)
                    {
                        g8=no8;
                        n=8;
                    }
                    else{
                        Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
                    }
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }



                double res = 0;



                if((g1<=10)&&(g2<=10)&&(g3<=10)&&(g4<=10)&&(g5<=10)&&(g6<=10)&&(g7<=10)&&(g8<=10)) {
                    res = (g1+g2+g3+g4+g5+g6+g7+g8) / n;
                    tv10.setText("Your CGPA is : " + res);
                    Toast.makeText(Calculator.this, "Your CGPA is : " + res, Toast.LENGTH_LONG).show();
                }
                else{
                    Toast.makeText(Calculator.this,"Please enter a valid GPA ",Toast.LENGTH_LONG).show();
                }

            }

        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                et2.setText("");
                et3.setText("");
                et4.setText("");
                et5.setText("");
                et6.setText("");
                et7.setText("");
                et8.setText("");
                tv10.setText("Your CGPA is : ");
                et1.setText("");

            }
        });


    }


}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 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">


    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Enter Your 1st Semester GPA"
                android:id="@+id/textView"
                android:textColor="#000000" />

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:maxLength="4"
                android:ems="10"
                android:id="@+id/editText" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Enter Your 2nd Semester GPA"
                android:id="@+id/textView2"
                android:textColor="#000000" />

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:maxLength="4"
                android:ems="10"
                android:id="@+id/editText2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Enter Your 3rd Semester GPA"
                android:id="@+id/textView3"
                android:textColor="#000000" />

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:ems="10"
                android:maxLength="4"
                android:id="@+id/editText3"
                android:layout_gravity="center_horizontal" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Enter Your 4th Semester GPA"
                android:id="@+id/textView4"
                android:textColor="#000000" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:ems="10"
                android:maxLength="4"
                android:id="@+id/editText4" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Enter Your 5th Semester GPA"
                android:id="@+id/textView5"
                android:textColor="#000000" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:ems="10"
                android:maxLength="4"
                android:id="@+id/editText5" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Enter Your 6th Semester GPA"
                android:id="@+id/textView6"
                android:textColor="#000000" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:maxLength="4"
                android:ems="10"
                android:id="@+id/editText6" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Enter Your 7th Semester GPA"
                android:id="@+id/textView7"
                android:textColor="#000000" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:maxLength="4"
                android:ems="10"
                android:id="@+id/editText7"
                android:layout_gravity="center_horizontal" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Enter Your 8th Semester GPA"
                android:id="@+id/textView8"
                android:textColor="#000000" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                android:ems="10"
                android:maxLength="4"

                android:id="@+id/editText8"
                android:layout_gravity="center_horizontal" />
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:layout_marginTop="20dp">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:text="Calculate"
                    android:id="@+id/button"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginRight="15dp"
                    android:layout_marginLeft="15dp" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Clear"
                    android:id="@+id/button2"
                    android:layout_gravity="right|center_horizontal"
                    android:layout_weight="0.5"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp" />
            </LinearLayout>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Your CGPA is :"
                android:id="@+id/textView10"
                android:layout_gravity="center_horizontal"
                android:textColor="#000000"
                android:layout_marginTop="25dp" />


        </LinearLayout>
    </ScrollView>

</LinearLayout>

已编辑的课程

package com.sabari.myapplication;


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


public class MainActivity extends AppCompatActivity {

    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    TextView tv7;
    TextView tv8;
    TextView tv10;
    EditText[] et=new EditText[8];
    Button b1;
    Button b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("CGPA Calculator");
        setContentView(R.layout.activity_main);



        tv1 = (TextView) findViewById(R.id.textView);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.textView5);
        tv6 = (TextView) findViewById(R.id.textView6);
        tv7 = (TextView) findViewById(R.id.textView7);
        tv8 = (TextView) findViewById(R.id.textView8);


        b1 = (Button) findViewById(R.id.button);
        b2 = (Button)findViewById(R.id.button2);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                et[0] = (EditText) findViewById(R.id.editText);
                et[1] = (EditText) findViewById(R.id.editText2);
                et[2]=  (EditText) findViewById(R.id.editText3);
                et[3] = (EditText) findViewById(R.id.editText4);
                et[4] = (EditText) findViewById(R.id.editText5);
                et[5] = (EditText) findViewById(R.id.editText6);
                et[6] = (EditText) findViewById(R.id.editText7);
                et[7] = (EditText) findViewById(R.id.editText8);


                tv10 = (TextView) findViewById(R.id.textView10);

                 private void initAndValidateEditText(final EditText editText,final int index){
                    editText.addTextChangedListener(new TextWatcher() {
                        double res = 0;
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            double value=Double.parseDouble(editText.getText().toString());

                            if(value>=5&&value<=10){
                                if(index!=7){
                                    et[index+1].setEnabled(true);

                                    initAndValidateEditText(et[0], 0);
                                    initAndValidateEditText(et[1], 1);
                                    initAndValidateEditText(et[2], 2);
                                    initAndValidateEditText(et[3], 3);
                                    initAndValidateEditText(et[4], 4);
                                    initAndValidateEditText(et[5], 5);
                                    initAndValidateEditText(et[6], 6);
                                    initAndValidateEditText(et[7], 7);

                                }
                            }else{
                                Toast.makeText(MainActivity.this,"Enter",Toast.LENGTH_LONG).show();
                                if(index!=7){
                                    et[index+1].setEnabled(false);
                                }
                            }
                            res = value;
                            tv10.setText("Your CGPA is : " + res);


                        }

                    });
            }



            }

        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                tv10.setText("Your CGPA is : ");
            }
        });


    }


}

3 个答案:

答案 0 :(得分:0)

您可以使用OnFocusChangeListener。让您的活动实现一个并覆盖接口方法:

@Override
public void onFocusChange(View arg0, boolean arg1) {

    // if focus lost
    if (!arg1) {
        EditText et = (EditText) arg0;
        if (et.getText().toString().equals(""))
             et.setError("wtf...fill it!");
    } 

}  

对于每个EditTexts,您必须执行以下操作:

et1.setOnFocusChangeListener(this); // this is your activity and it must implement View.OnFocusChangeListener interface

答案 1 :(得分:0)

else没有被执行,因为你的代码没有那么远。尝试从空字符串解析double会抛出异常并跳转到catch块。您真正想要做的是在尝试解析双精度之前检查EditText中是否存在某些内容。

if(et1.getText().length() == 0)
{
    Toast.makeText(Calculator.this,"All fields must be filled in",Toast.LENGTH_LONG);
}
else
{
    try
    {
        no1 = Double.parseDouble(et1.getText().toString());
        if(no1>=5&&no1<=10)
        {
            g1=no1;
            n=1;
        }
        else
        {
            Toast.makeText(Calculator.this,"Please enter a valid GPA",Toast.LENGTH_LONG);
        }
    }
    catch (NumberFormatException e)
    {
        e.printStackTrace();
        Toast.makeText(Calculator.this,"Failed to parse GPA",Toast.LENGTH_LONG);
    }
}

答案 2 :(得分:0)

您可以实现目标的方法之一是默认禁用edittexts,然后在用户输入有效值时逐步启用它们。首先,按如下方式定义您的edittext XML:

开始定义您的edittexts,如下所示我们可以轻松地引用它们:

// This variable will be a class variable so that any method can access it
EditText[] editTexts = new EditText[8];

// In your onCreate() init it
editTexts[0] = (EditText) findViewById(R.id.editText);
editTexts[1] = (EditText) findViewById(R.id.editText2);
editTexts[2] = (EditText) findViewById(R.id.editText3);
editTexts[3] = (EditText) findViewById(R.id.editText4);
editTexts[4] = (EditText) findViewById(R.id.editText5);
editTexts[5] = (EditText) findViewById(R.id.editText6);
editTexts[6] = (EditText) findViewById(R.id.editText7);
editTexts[7] = (EditText) findViewById(R.id.editText8);

现在,我们在您的类中创建一个方法来执行我们的验证,同时为我们启用下一个edittext:

private void initAndValidateEditText(final EditText editText, final int index) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }


            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }


            @Override
            public void afterTextChanged(Editable s) {
                int value = Integer.parseInt(editText.getText().toString());

                if (value >= 5 && value <= 10) {
                    if (index != 7) {
                        editTexts[index + 1].setEnabled(true);
                    }
                } else {
                    Toast.makeText(Calculator.this, "wtf!...enter a valid GPA", Toast.LENGTH_LONG).show();
                    if (index != 7) {
                        editTexts[index + 1].setEnabled(false);
                    }
                }
            }
        }

仅在onCreate()内,将每个edittext传递给此方法:

initAndValidateEditText(editTexts[0], 0);
initAndValidateEditText(editTexts[1], 1);
initAndValidateEditText(editTexts[2], 2);
initAndValidateEditText(editTexts[3], 3);
initAndValidateEditText(editTexts[4], 4);
initAndValidateEditText(editTexts[5], 5);
initAndValidateEditText(editTexts[6], 6);
initAndValidateEditText(editTexts[7], 7);

现在每个edittex都已经过验证(除了最后一个,如果索引在上面的方法中是7,可以通过启用提交按钮来处理),所以你不需要做任何其他验证你的onClick()。如果您遇到任何问题,请告诉我。

<强> [编辑]

您的整个班级文件如下:

package com.sabari.myapplication;


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


public class MainActivity extends AppCompatActivity {

    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    TextView tv7;
    TextView tv8;
    TextView tv10;
    EditText[] et = new EditText[8];
    Button b1;
    Button b2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("CGPA Calculator");
        setContentView(R.layout.activity_main);


        tv1 = (TextView) findViewById(R.id.textView);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.textView5);
        tv6 = (TextView) findViewById(R.id.textView6);
        tv7 = (TextView) findViewById(R.id.textView7);
        tv8 = (TextView) findViewById(R.id.textView8);

        et[0] = (EditText) findViewById(R.id.editText);
        et[1] = (EditText) findViewById(R.id.editText2);
        et[2] = (EditText) findViewById(R.id.editText3);
        et[3] = (EditText) findViewById(R.id.editText4);
        et[4] = (EditText) findViewById(R.id.editText5);
        et[5] = (EditText) findViewById(R.id.editText6);
        et[6] = (EditText) findViewById(R.id.editText7);
        et[7] = (EditText) findViewById(R.id.editText8);

        initAndValidateEditText(et[0], 0);
        initAndValidateEditText(et[1], 1);
        initAndValidateEditText(et[2], 2);
        initAndValidateEditText(et[3], 3);
        initAndValidateEditText(et[4], 4);
        initAndValidateEditText(et[5], 5);
        initAndValidateEditText(et[6], 6);
        initAndValidateEditText(et[7], 7);


        tv10 = (TextView) findViewById(R.id.textView10);


        b1 = (Button) findViewById(R.id.button);
        b2 = (Button) findViewById(R.id.button2);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // Calculate CGPA here by et[x].getText() methods

                tv10.setText("Your CGPA is : ");
            }

        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });


    }


    private void initAndValidateEditText(final EditText editText, final int index) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }


            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }


            @Override
            public void afterTextChanged(Editable s) {
                double value = Double.parseDouble(editText.getText().toString());

                if (value >= 5 && value <= 10) {
                    if (index != 7) {
                        et[index + 1].setEnabled(true);
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Enter value", Toast.LENGTH_LONG).show();
                    if (index != 7) {
                        et[index + 1].setEnabled(false);
                    }
                }
            }

        });
    }


}