【问题标题】:Test if Multiple EditTexts are empty测试多个 EditTexts 是否为空
【发布时间】:2011-02-15 15:47:04
【问题描述】:

假设我在一个应用程序中有多个 EditText。当我点击一个按钮时,我想测试哪些 EditTexts 是空的,哪些 EditTexts 有内容。我还想要内容不为空的 EditText 的 id。我该怎么做?有人可以为按钮单击处理程序编写代码。我的六个 EditText 的 id 分别是 FirstString、SecondString、ThirdString、... Button 的 id 是 button。

【问题讨论】:

    标签: java android events android-edittext


    【解决方案1】:

    只需在设置视图时为保存它们的类创建一个数组,并在按下按钮时检查每一个。如果没有看到你已经拥有的东西,就无法真正编写代码。

    【讨论】:

      【解决方案2】:

      你总是可以遍历父视图的子视图:

      View parent = findViewById(R.id.parentlayout_id);
      for(int i = 0; i < parent.getChildCount(); i++){
         View v = parent.getChildAt(i);
         if(v instanceof EditText)
            //cast it and check the text here...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-09
        • 2011-02-27
        • 2018-08-10
        • 2011-09-17
        • 2019-01-03
        • 1970-01-01
        • 1970-01-01
        • 2015-09-03
        相关资源
        最近更新 更多