如何验证不允许单独使用“。”的十进制输入和空场?

How to validate decimal input not allowing alone "." and empty field?

我正在 Android Studio 上 Java 中编写一个计算器,如果用户用点“.”调用结果,应用程序会崩溃。单独或让 EditText 字段为空白。 我正在寻找一种解决方案,以防止这两种情况在这三个领域中同时或单独发生。 我已经尝试过 TextWatcher 和 if/else 但没有成功。

设计editText字段的.xml文件已经设置为decimalNumber。

我已经试过了:

if(myfieldhere.getText().toString().equals(".")){myfieldhere.setText("0");}

对于每个 "valor line",如果一切正常,则对于 "finalresult" 行。都在 setOnClickListener 块内。这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.peso_layout);
        result = findViewById(R.id.layresult);
        conc = findViewById(R.id.layconc);
        dose = findViewById(R.id.laydose);
        peso = findViewById(R.id.laypeso);
        calc = findViewById(R.id.laycalcpeso);

    calc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        float valor1 = Float.parseFloat(peso.getText().toString());
        float valor2 = Float.parseFloat(conc.getText().toString());
        float valor3 = Float.parseFloat(dose.getText().toString());
        float finalresult = valor1 * valor2 * valor3;
        result.setText("The result is: " + finalresult);
                }

    });
    }

理想的输出应该是应用程序不会在这两种情况发生时崩溃,并向用户发送输入无效的错误消息。 我收到的是应用程序崩溃。 非常感谢你。我是 Java 的初学者,几天来一直在努力解决这个问题。

亲爱的朋友,您直接尝试将字符串输入转换为浮点数,然后在您的校验值之后执行您的代码,如下所示。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

EditText edt1,edt2;
TextView txtans;
Button btnsum;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edt1=findViewById(R.id.edt1);
    edt2=findViewById(R.id.edt2);
    txtans=findViewById(R.id.txtans);
    btnsum=findViewById(R.id.btnsum);
    btnsum.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    if(v.getId()==R.id.btnsum){
        float n1,n2;
        String value1=edt1.getText().toString();
        String value2=edt2.getText().toString();

        if(value1.equals("") || value1.equals(".")){
            n1=0;
        }else {
            n1= Float.parseFloat(value1);
        }
        if(value2.equals("")|| value2.equals(".")){
            n2=0;
        }else{
            n2= Float.parseFloat(value2);
        }
        float ans=n1+n2;


        txtans.setText(ans+"");

    }
}

}

参见上面的代码,F首先从 edittext 中获取值,然后检查它是否包含 null 或“.”。在里面。如果它包含则将 0.0 值存储在某个变量中。然后在求和后显示在文本框中。

calc.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String myvalor = myfieldhere.getText().toString();
        if(myvalor.equals(".") || myvalor.isEmpty())
        {
            // toast error : incorrect value
            return;
        }
        try
        {
            float valor1 = Float.parseFloat(peso.getText().toString());
            float valor2 = Float.parseFloat(conc.getText().toString());
            float valor3 = Float.parseFloat(dose.getText().toString());
            float finalresult = valor1 * valor2 * valor3;
            result.setText("The result is: " + finalresult);
        }
        catch(Exception exp){// toast with exp.toString() as message}
    }
});

使用 TextUtils 检查空字符串更好

if(TextUtils.isEmpty(peso.getText().toString())||
TextUtils.isEmpty(conc.getText().toString())||
TextUtils.isEmpty(dose.getText().toString())){
   return;
 }