验证编辑文本

时间:2014-04-11 20:12:02

标签: android

我想在点击提交按钮时验证包含数字的编辑文本 但不适用

这是我的代码

    alert = new AlertDialog.Builder(Map.this);
    alert.setTitle("About");
    alert.setMessage("You have to enter a number of Boundaries and Kilometers");
    alert.setIcon(R.drawable.tick);
    alert.setNegativeButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // Write your code here to execute after dialog closed
        Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
        }
    });

noOfCircles=(EditText)findViewById(R.id.boundary);      
Kilometers=(EditText) findViewById(R.id.diameter);

    submit.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
       if(noOfCircles.getText().toString().equalsIgnoreCase("helloworld")||Kilometers.getText().toString().equalsIgnoreCase("helloworld")){
    alert.show();
        }
        }
        });

Here is my picture to provide you what I need

    }

1 个答案:

答案 0 :(得分:0)

从警告消息中,您似乎正在尝试检查是否已输入两个值。如果是这种情况,请尝试以下代码:

    submit.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            String circles = noOfCircles.getText().toString();
            String kilometers = Kilometers.getText().toString();
            if(circles.equalsIgnoreCase("") || kilometers.equalsIgnoreCase("")){
                alert.setMessage("Please enter both values!");
            }
            else{
                if(circles.equalsIgnoreCase(kilometers)){
                    alert.setMessage("Values are equal");
                    //do your own stuff here
                }
            }
            alert.show();
        }
    });