Java android:检查EditText是否为空
我想检查我的edittext并给出条件,如果我在edittext中输入了一些内容,它将更改我的imageview,这是我的代码:Java android:检查EditText是否为空,java,android,android-edittext,Java,Android,Android Edittext,我想检查我的edittext并给出条件,如果我在edittext中输入了一些内容,它将更改我的imageview,这是我的代码: if (nama_pp.getText().toString().length()==0){ ImageView image_status=(ImageView)polis.findViewById(R.id.image_status_1); image_status.setImageResource(R.drawab
if (nama_pp.getText().toString().length()==0){
ImageView image_status=(ImageView)polis.findViewById(R.id.image_status_1);
image_status.setImageResource(R.drawable.espaj_yellow_checklist);
}else {
ImageView image_status=(ImageView)findViewById(R.id.image_status_1);
image_status.setImageResource(R.drawable.espaj_gray_checklist);
}
我这里有问题,我的图像视图没有改变。。。。此代码是否为真 如果要检查EditText是否为空,请执行以下操作:
if (nama_pp == null)
但我不认为那是你想要的。您希望查看其中的字符串是否为null,因此可以尝试以下操作:
if (nama_pp.getText().toString() == null || nama_pp.getText().toString().length()==0){
我想在这种情况下你不需要or,但是如果你删除EditText中的所有字符,我不知道Android是否会留下一个空字符串。如果我正确阅读了你的文章,你想检查用户键入的EditText是否为空。如果这是正确的,那么尝试以下方法 调用setContentView后,该行需要出现在onCreate方法中:
ImageView image_status=(ImageView)polis.findViewById(R.id.image_status_1);
然后,在分配nama_pp后添加此代码:
nama_pp.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence p1, int p2, int p3, int p4)
{
// TODO: Implement this method
}
public void afterTextChanged(Editable p1)
{
// TODO: Implement this method
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if(s.length() == 0)
{
image_status.setImageResource(R.drawable.espaj_yellow_checklist);
}
else
{
image_status.setImageResource(R.drawable.espaj_gray_checklist);
}
}
}
);
我最近研究了如何在片段之间传递数据,并使用了这个选项,我更喜欢它,因为它易于阅读和清理
if(message.text.isEmpty()) {
Toast.makeText(activity, "Message goes in here ..." , Toast.LENGTH_SHORT).show()
}else { activity ..... whatever you wanna do
处理希望跳过重要数据的用户。我认为您需要更清楚地解释一下。问题是什么?这将起作用,取决于您将代码放置在何处。您可能需要一个
TextWatcher
您可以使用TextUtils.isEmpty(nama_pp.getText())
来检查值是null
还是空的。@codeMagic当我在edittext中键入内容时,我希望从ImageView得到的响应是change@AoyamaNanami看看Mike M.给你的答案,因为这是正确的方法