gpt4 book ai didi

android - 检查空的edittext android

转载 作者:行者123 更新时间:2023-11-29 01:42:38 25 4
gpt4 key购买 nike

我有一个输入类型设置为数字的编辑文本。我想检查 edittext 是否为空。

 <EditText
android:id="@+id/noOfTranset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Enter number:"
android:inputType="number"
android:textColor="@android:color/black"
android:maxLength="2"/>

下面是我试过但不检查空编辑文本的代码。我在这里错过了什么?谢谢。

String numTrans = et1.getText().toString();
int transaction = Integer.parseInt(numTrans);



if(numTrans.trim().length() == 0 || numTrans.equals("") || numTrans == null){

// none of the above conditions check for empty edittext

}

最佳答案

问题是您试图将空字符串 转换为Integer。当输入文本为 nullempty 时,Integer.parseInt 将抛出 NumberFormatException

更改代码以仅在输入文本不为空时才将字符串解析为整数

if(!TextUtils.isEmpty(numTrans)){
int transaction = Integer.parseInt(numTrans);
// do your other stuff here
}

关于android - 检查空的edittext android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23530778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com