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

2024-03-12

在此我有八个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 : ");
            }
        });


    }


}

您可以使用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!");
    } 

}  

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

et1.setOnFocusChangeListener(this); // this is your activity and it must implement View.OnFocusChangeListener interface
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐