我的代码中有八个EditText
个。我需要允许用户仅在前一个EditText
填写时才在EditText
中输入数据,否则我必须提示前一个EditText
中的一个是空白的。我如何实现这一目标?
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(et1.getText().length()!=0||
(et1.getText().length()!=0&&et2.getText().length()!=0)||
(et1.getText().length()!=0&&et2.getText().length()!=0&&et3.getText().length()!=0)||
(et1.getText().length()!=0&&et2.getText().length()!=0&&et3.getText().length()!=0&&et4.getText().length()!=0)||
(et1.getText().length()!=0&&et2.getText().length()!=0&&et3.getText().length()!=0&&et4.getText().length()!=0&&et5.getText().length()!=0)||
(et1.getText().length()!=0&&et2.getText().length()!=0&&et3.getText().length()!=0&&et4.getText().length()!=0&&et5.getText().length()!=0&&et6.getText().length()!=0)||
(et1.getText().length()!=0&&et2.getText().length()!=0&&et3.getText().length()!=0&&et4.getText().length()!=0&&et5.getText().length()!=0&&et6.getText().length()!=0&&et7.getText().length()!=0)||
(et1.getText().length()!=0&&et2.getText().length()!=0&&et3.getText().length()!=0&&et4.getText().length()!=0&&et5.getText().length()!=0&&et6.getText().length()!=0&&et7.getText().length()!=0&&et8.getText().length()!=0)) {
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 the required fields` ",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("");
}
});
}
}
答案 0 :(得分:0)
您可以使用关键字return
来阻止代码运行。因此,首先检查EditText
是否为空,然后您将显示您的消息并停止运行代码。
示例
if(mEditText.getText().toString().equals(""){
Toast.makeText(mContext,"Fill Text Boxes Accordingly",Toast.LENGHT_SHORT).show();
//stop the code from running
return;
}