猿问

计算填充的 EditText 字段

我正在构建一个应用程序,它有多个EditText字段和一个Button.
有关信息,Button应该Activity根据填充的EditText字段打开一个新的(我还没有到那个),

因此,假设
1) 如果第 3 个Editext字段具有某个值,则按钮将打开第 3 个Activity
2) 如果第 4 个Editext字段具有某个值,则按钮将打开 4th Activity
这适用于每个EditText领域。

问题是,如何计算填充的 editText 字段?


肥皂起泡泡
浏览 164回答 3
3回答

白衣非少年

你可以像这样在 Activity 中做到这一点..但是你必须添加android:tag="et"在布局的所有 Edittext 字段中..相应地更改代码中的父布局类型。它按测试工作..public class MainActivity extends AppCompatActivity {&nbsp;int count=0;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; LinearLayout parentLayout = (LinearLayout) findViewById(R.id.linearLayout);&nbsp; &nbsp; &nbsp; &nbsp; int elements = parentLayout.getChildCount();&nbsp; &nbsp; &nbsp; &nbsp; View v = null;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<elements; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v = parentLayout.getChildAt(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(v.getTag()!=null && v.getTag().equals("et")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count= count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在计数的基础上,您可以做出决定。

SMILET

我目前没有 Android SDK,所以可能会有一些错误,但你可以尝试这样的事情:-long numberOfFilledFields = Stream.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editText1.getText().toString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editText2.getText().toString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editText3.getText().toString()) //you can add as many as you want here&nbsp; &nbsp; &nbsp; &nbsp; .filter(s -> !s.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; .count();switch (numberOfFilledFields) {&nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; //start activity 1&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; //start activity 2&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; .}

holdtom

您可以使用一些标志来设置填充的 editText 字段的计数。将所有 editText 引用添加到 List。并使用以下方法获取计数。int count =0;private int getEditTextViewCount(List<EditText> editTexts){&nbsp; &nbsp; for(EditText editText : editTexts){&nbsp; &nbsp; &nbsp; &nbsp; if(!editText.getText().toString().isEmpty()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return count;}
随时随地看视频慕课网APP

相关分类

Java
我要回答