在android studio中使用小于,大于和等于条件。 [英] Using of less than , greater than and equals condition in android studio.

查看:1427
本文介绍了在android studio中使用小于,大于和等于条件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本视图中显示默认值,因为我在编辑文本中输入一个数字我需要验证说输入的数字不应该大于或等于它,这里有任何帮助吗?到目前为止,我已经使用了> =和>但没有工作......



我尝试过:



I have a default value show in a Text View ,as i enter a number in an Edit Text i need a validation saying the Entered number shouldn't be greater than or equal to it, any help here?? So far I've used >= and > but didn't work ......

What I have tried:

@Override
            public void onClick(View v) {
                if (amt.getText().toString().trim().isEmpty()){
                    Toast.makeText(myView.getContext(), "Enter Valid Amount", Toast.LENGTH_LONG).show();
                    //isValidDecimalNumber(amt);
                }
                else if ( (amt.getText().toString().trim().length() >=  txt1.getText().toString().trim().length())){

                }
                else {
                     tot = Double.parseDouble(amt.getText().toString());
                     tt=(tot / 100.0f) * 3+tot;
                    loadProducts2();
                }

            }

推荐答案

else if ( (amt.getText().toString().trim().length()   txt1.getText().toString().trim().length())){



这两条线应该做什么?



检查文本框中是否有文字后,您应该尝试转换它。然后,您可以测试它是否在可接受的值范围内。您还应该将转换包装在try / catch块中以捕获无效输入。



[edit]


What are these two lines supposed to be doing?

Once you have checked that there is some text in your textbox, then you should try to convert it. You can then test that it lies within the range of acceptable values. You should also wrap the conversion in a try/catch block to capture invalid input.

[edit]

public void onClick(View v) {
string strAmount = amt.getText().toString().trim();
if (strAmount.isEmpty()){
    Toast.makeText(myView.getContext(), "Enter Valid Amount", Toast.LENGTH_LONG).show();
}
else {
    try {
        tot = Double.parseDouble(strAmount);
        if (tot < 0 || tot > maxAmount) {
            // show invalid number
        }
        tt=(tot / 100.0f) * 3+tot;
        loadProducts2();
    catch (NumberFormatException ne) {
        Toast.makeText(myView.getContext(), strAmount + " is not a valid number", Toast.LENGTH_LONG).show();
    }
}



[/ edit]


[/edit]


你应该编写更清晰的代码,例如
You should write clearer code like
public void onClick(View v) {
    String text = amt.getText().toString().trim();

    if (text.isEmpty()){
    } else {
        int value = Double.parseDouble(text);
        // deals with value

这有助于此找到更好的代码很多。



你的else if子句看起来很奇怪。重新审视并重新考虑代码行。

This helps a lot in finding better code.

Your else if clause looks very strange. Revisit and rethink that code lines.


这篇关于在android studio中使用小于,大于和等于条件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆