如果用户在第二个EditText中输入0,则显示警报

时间:2014-03-19 11:21:28

标签: android android-edittext alert

我正在制作一个计算器应用程序,其中我想显示一个警告是用户将任何东西除以零.REST一切正常但我用户将任何东西除以零,应该提出警告

package com.example.calculator;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Calci extends Activity {
    TextView t1;
    EditText e1, e2;
    Button add, sub, mul, div;
    Context c = this;

    String b, a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calci);
        e1 = (EditText) findViewById(R.id.EditText01);
        e2 = (EditText) findViewById(R.id.EditText02);
        add = (Button) findViewById(R.id.add);
        sub = (Button) findViewById(R.id.sub);
        mul = (Button) findViewById(R.id.mul);
        div = (Button) findViewById(R.id.div);
        t1 = (TextView) findViewById(R.id.textView1);
        div.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                a = e1.getText().toString();
                b = e2.getText().toString();
                AlertDialog.Builder a1 = new AlertDialog.Builder(c);
                try{
                if (a.equals("") || b.equals("")) {

                    // AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                    // Setting Dialog Title
                    a1.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    a1.setMessage("PLEASE ENTER SOMETHING");

                    a1.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int button1) {
                                    // if this button is clicked, close
                                    // current activity
                                    dialog.cancel();
                                }

                            });

                    // Showing Alert Message
                    AlertDialog alertDialog = a1.create();
                    a1.show();

                }
                }
                catch (ArithmeticException e) {
                Log.d("ERROR", "msg");
                }

//              if (b.equals(0)) {
//                  a1.setTitle("INVALID");
//                  a1.setMessage("CAnnot divide by zero");
//                  a1.setPositiveButton("YES",
//                          new DialogInterface.OnClickListener() {
//                              public void onClick(DialogInterface dialog,
//                                      int button1) {
//                                  // if this button is clicked, close
//                                  // current activity
//                                  dialog.cancel();
//                              }
//
//                          });
//                  AlertDialog alertDialog = a1.create();
//                  a1.show();



                    Log.d("sadasd", "msg");
                    int result = Integer.parseInt(a) / Integer.parseInt(b);
                    t1.setText(Integer.toString(result));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(add.getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);


            }

        });

        mul.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                a = e1.getText().toString();
                b = e2.getText().toString();
                if (a.equals("") || b.equals("")) {

                    AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                    // Setting Dialog Title
                    a1.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    a1.setMessage("PLEASE ENTER SOMETHING");

                    a1.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int button1) {
                                    // if this button is clicked, close
                                    // current activity
                                    dialog.cancel();
                                }

                            });

                    // Showing Alert Message
                    AlertDialog alertDialog = a1.create();
                    a1.show();

                }

                else {
                    Log.d("sadasd", "msg");
                    int result = Integer.parseInt(a) * Integer.parseInt(b);
                    t1.setText(Integer.toString(result));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(add.getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);
                }

            }

        });

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                a = e1.getText().toString();
                b = e2.getText().toString();
                if (a.equals("") || b.equals("")) {

                    AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                    // Setting Dialog Title
                    a1.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    a1.setMessage("PLEASE ENTER SOMETHING");

                    a1.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int button1) {
                                    // if this button is clicked, close
                                    // current activity
                                    dialog.cancel();
                                }

                            });

                    // Showing Alert Message
                    AlertDialog alertDialog = a1.create();
                    a1.show();

                }

                else {
                    Log.d("sadasd", "msg");
                    int result = Integer.parseInt(a) - Integer.parseInt(b);
                    t1.setText(Integer.toString(result));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(add.getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);
                }

            }

        });

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                a = e1.getText().toString();
                b = e2.getText().toString();
                if (a.equals("") || b.equals("")) {

                    AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                    // Setting Dialog Title
                    a1.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    a1.setMessage("PLEASE ENTER SOMETHING");

                    a1.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int button1) {
                                    // if this button is clicked, close
                                    // current activity
                                    dialog.cancel();
                                }

                            });

                    // Showing Alert Message
                    AlertDialog alertDialog = a1.create();
                    a1.show();

                }

                else {
                    Log.d("sadasd", "msg");
                    int result = Integer.parseInt(a) + Integer.parseInt(b);
                    t1.setText(Integer.toString(result));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(add.getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);
                }

            }

        });

    }
}

我正在制作一个计算器应用程序,我想在其中显示警告,用户将任何东西除以零。

4 个答案:

答案 0 :(得分:0)

勾选Div按钮onClick()函数。

String divd = edittext.getText().toString();
if(divd.equals("0")){
Toast.makeText(ActivityName.this, "You are dividing by 0", Toast.LENGTH_LONG).show();
return;
}

答案 1 :(得分:0)

简单,只需添加一个条件

div.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                a = e1.getText().toString();
                b = e2.getText().toString();
                AlertDialog.Builder a1 = new AlertDialog.Builder(c);
                try{
                if (a.equals("") || b.equals("") || b.equals("0")) {

                 ........
                .........

或者你可以这样做

if (a.equals("") || b.equals("") {
     .......
     .......
}else if (b.equals("0")){
              AlertDialog.Builder a1 = new AlertDialog.Builder(calci.this);
              a1.setTitle("INVALID");
              a1.setMessage("CAnnot divide by zero");
              a1.setPositiveButton("YES",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog,
                                  int button1) {
                              // if this button is clicked, close
                              // current activity
                              dialog.cancel();
                          }

                      });
              AlertDialog alertDialog = a1.create();
              a1.show();
}else{
 ....  
 ....  
}

答案 2 :(得分:0)

 a = e1.getText().toString();
            b = e2.getText().toString();
            AlertDialog.Builder a1 = new AlertDialog.Builder(c);
            try{
            if  b.equals("0")) {

                // AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                // Setting Dialog Title
                a1.setTitle("Alert Dialog");

                // Setting Dialog Message
                a1.setMessage("You are dividing by 0");

                a1.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int button1) {
                                // if this button is clicked, close
                                // current activity
                                dialog.cancel();
                            }

                        });

                // Showing Alert Message
                AlertDialog alertDialog = a1.create();
                a1.show();

            }
            }
            catch (ArithmeticException e) {
            Log.d("ERROR", "msg");
            }

答案 3 :(得分:0)

尝试实现EditText Change Listener

     textMessage = (EditText)findViewById(R.id.textMessage);
    textMessage.addTextChangedListener(new TextWatcher(){
   public void afterTextChanged(Editable s) {


  }
     public void beforeTextChanged(CharSequence s, int start, int count, int after){}
     public void onTextChanged(CharSequence s, int start, int before, int count){
      if(s.toString.equal("0")){
      Toast.makeText(c, "You are dividing by 0", Toast.LENGTH_LONG).show();
                                }
    }
                            });