在java中使用编辑文本和数字的问题

时间:2017-12-31 02:13:19

标签: java android

我正在自己学习java,我正在创建一个计算区域的应用程序,但是当我输入一位数字时,应用程序说我没有放任何东西。但它适用于小数和2位数字。任何人都可以帮我弄清楚为什么会这样吗?那就是代码:

public void calcRetangulo (View view){
    EditText compRet = (EditText) findViewById(R.id.compRetangulo);
    EditText ladoRet = (EditText) findViewById(R.id.ladoRetangulo);

    if (compRet.getText().toString().matches("") || compRet.getText().toString().matches(".") && ladoRet.getText().toString().matches("") || ladoRet.getText().toString().matches(".")){

        LayoutInflater er = getLayoutInflater();

        View erro = er.inflate(R.layout.erro, null);

        erro.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                alertaErro.dismiss();
            }
        });

        AlertDialog.Builder builderErro = new AlertDialog.Builder(this);
        builderErro.setView(erro);
        alertaErro = builderErro.create();
        alertaErro.show();

        compRet.setText("");
        ladoRet.setText("");

    } else{

        float comp = Float.parseFloat(compRet.getText().toString());
        float lado = Float.parseFloat(ladoRet.getText().toString());

        float area = comp * lado;

        LayoutInflater li = getLayoutInflater();

        View result = li.inflate(R.layout.layout, null);
        final TextView resultado = (TextView) result.findViewById(R.id.resultado);

        resultado.setText(String.format("A = c x l\nA = "+ comp +" x " + lado +"\nA = "+ area));

        result.findViewById(R.id.fechar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                alertaResult.dismiss();
                resultado.setText("");

            }
        });

        AlertDialog.Builder builderResult = new AlertDialog.Builder(this);
        builderResult.setView(result);
        alertaResult = builderResult.create();
        alertaResult.show();

        compRet.setText("");
        ladoRet.setText("");
    }
}

3 个答案:

答案 0 :(得分:1)

java中的

String.matches是一个正则表达式。因此matches(".")将匹配任何1位数字符串(。是正则表达式中的通配符)。如果要匹配文字字符串".",请使用matches("\.")

答案 1 :(得分:0)

使用EditText文本的长度。 matches()用于正则表达式。

答案 2 :(得分:0)

您应该使用 $vendorTableName= with(new Vendors())->getTable(); $categoryTableName= with(new Excategories())->getTable(); $orders =Checks::select(["checks.*","vendors.vendor_name","excategories.category_name"])->between($start, $end)->join($vendorTableName,$vendorTableName.".vendor_id","=","checks.vendor_id")->join($categoryTableName,$categoryTableName.".category_id","=","checks.category_id");