【问题标题】:multiple if statement with different conditions具有不同条件的多个 if 语句
【发布时间】:2014-04-21 02:50:46
【问题描述】:

我尝试检查EditText 上的用户输入,如果有一个或多个空值,验证图像将变为红色,并且在填充所​​有字段之前不会移动页面。在我的情况下,我使用了多个具有不同条件的 if 语句,但是 我遇到了一个问题,当我输入两个EditText 而另一个EditText 为空时,页面从当前页面移动到新页面,而它不应该被移动。 这是我的代码:

if (nama_pp.getText().toString().length()==0){img_namalengkap_pp.setImageResource(R.drawable.espaj_red_checklist);}
            if (ibu_pp.getText().toString().length()==0){img_namaibu_pp.setImageResource(R.drawable.espaj_red_checklist);}
            if (nomor_bukti_pp.getText().toString().length()==0){img_nomeridentitas_pp.setImageResource(R.drawable.espaj_red_checklist);}
            if (tempat_pp.getText().toString().length()==0){img_tempat_pp.setImageResource(R.drawable.espaj_red_checklist);}
            if (ttl_pp.getText().toString().length()==0){img_tgllahir_pp.setImageResource(R.drawable.espaj_red_checklist);}
            if (alamat_pp.getText().toString().length()==0){img_alamat_pp.setImageResource(R.drawable.espaj_red_checklist);}
            if (kota_pp.getText().toString().length()==0){img_kota_pp.setImageResource(R.drawable.espaj_red_checklist);}
            if (kdtlp1_pp.getText().toString().length()==0){img_telepon_pp.setImageResource(R.drawable.espaj_red_checklist);}
            if (telp1_pp.getText().toString().length()==0){img_telepon_pp.setImageResource(R.drawable.espaj_red_checklist);}
else {
    getFragmentManager().beginTransaction().replace(R.id.frame_container, new TertanggungPolis()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}

有没有更快的方法来编写类似的代码以及解决我的问题的最佳解决方案?非常感谢。

【问题讨论】:

  • 提示:使用 Android API 的 TextUtils.isEmpty(string) 检查它是否为空。
  • 对我来说似乎是 codereview.stackexchange.com 的问题。因为您的代码有效,所以您只想优化。
  • @donfuxx 足够接近,但代码本身与最后一个 if-block 存在问题(逻辑错误)。我假设 Code Review 只接受有效且正确的代码?
  • 好吧,如果也有bug那么这里就好了;-)
  • @AndrewT.谢谢你的提示

标签: java android if-statement


【解决方案1】:

您可以将所有编辑文本放在一个数组列表中,将相应的图像视图放在另一个列表中。

对于空字符串,我们可以使用TextUtils.isEmpty(string),如果字符串为空或空则返回true。

试试这个:

ArrayList<EditText> editTexts = new ArrayList<EditText>();

editTexts.add(nama_pp);
editTexts.add(editetext2);

ArrayList<ImageView> imageViews =  new ArrayList<ImageView>();
imageViews.add(image1);
imageVioew.add(Image2);

boolean nextPage = true;

for (int i = 0; i < editTexts.length();i++) 
    if (editText[i].isEmpty(editText[i].getText.toString())) {
        imageViews[i].setImageResource(R.drawable.espaj_red_checklist);
        nextPage = false;
    }
}

在转到下一页之前,您可以检查以下内容:

if (nextPage) {
//Move to next page
}

【讨论】:

  • 您愿意解释一下您的代码以及为什么应该使用它吗?
  • @ZohraKhan 我的问题不仅是使我的代码更简单的更快方法,当我输入两个 EditText 而另一个 EditText 为空时,页面从当前页面移动到新页面,当它不应移动
  • 嗨@ZohraKhan,你能进入Android Coders 聊天室吗..??
【解决方案2】:

您可以将所有对象放在一个列表中,然后像这样遍历该列表:

ArrayList<EditText> editTexts = new ArrayList<>(9);

editTexts.add(nama_pp);
editTexts.add(...);
boolean ready = true;
for (EditText editText : editTexts)
    if (editText.getText().toString().length() == 0) { ready = false; break; }
if (ready)
    getFragmentManager().beginTransaction().replace(R.id.frame_container, new TertanggungPolis()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
else img_telepon_pp.setImageResource(R.drawable.espaj_red_checklist);

至少打字少了。

Sebastien Bianchi 的想法也不错。

【讨论】:

  • 如果我使用你的方式,我对ImageView 有不同的名称,我认为它只能用于具有相同ImageViewEditText,但我认为你的想法与给boolean逻辑,不错,我试试
【解决方案3】:

为什么不加一个textWatcher呢?更客观,不需要考虑条件,你只要问每个EditText你输入的是否有效。工作量少,扩展性好。你永远不会打扰有这么多的edittexts

【讨论】:

    【解决方案4】:

    它正在移动到下一页,因为您的 else 仅与最后一个 if 匹配。所以只要最后一个if测试失败,就会跳转到下一页。

    一个简单的解决方法是用else if 替换除第一个if 语句之外的所有语句:

    if (condition1) { 
    } else if (condition2) { 
    } else {
        getFragmentManager() ...
    }
    

    【讨论】:

      【解决方案5】:

      您可以通过使用|| 并创建isEmpty() 方法使其更具可读性:

      private boolean isEmpty(EditText et) {
          return et.getText().toString().length() == 0;
      }
      

      然后

      if (isEmpty(ibu_pp) || isEmpty(nomor_bukti_pp) || ...  ) {
          img_telepon_pp.setImageResource(R.drawable.espaj_red_checklist);
      }
      

      【讨论】:

      • 我有不同的名字ImageView 如果我使用你的方式,我认为它只能用于具有相同ImageViewEditText
      • 然后你可以在不同的 if 块中进行分隔,但是你可以做的就是让它更具可读性。
      • 是的,这就是我之前所做的,我将它分成不同的 if 块。
      猜你喜欢
      • 2014-10-20
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      • 2016-12-13
      • 2019-10-20
      相关资源
      最近更新 更多