简体   繁体   English

如果在 Android Studio 中有一个或多个为空,如何从多个 editText 中获取数据并显示在 textView 中?

[英]How to get data from multiple editText and show in textView if one or more is empty in Android Studio?

In my app there are two EditText named name and age , a TextView named showData and a Button.在我的应用程序中有两个名为nameage的 EditText,一个名为showData的 TextView 和一个 Button。 Basically what I want to do is when a user enters the data in the EditText and clicks the Button the TextView will show the output like this基本上我想要做的是当用户在 EditText 中输入数据并单击按钮时,TextView 将像这样显示 output

My name is name我的名字是name
I'm age years old我今年age

So for doing this I used If Else inside a setOnClickListener and wrote the code like this,所以为了做到这一点,我在setOnClickListener中使用了If Else并编写了这样的代码,


if (name.isEmpty() && age.isEmpty()) {
    showData.setText(null);
}


else if (age.isEmpty()) {
    showData.setText("My name is " + name);
}

else if (name.isEmpty()) {
    showData.setText("I'm " + age + " years old");
}

else{
    showData.setText("My name is " + name + 
                     "\n I'm " + age + " years old");
}

I'm a beginner in both Java and Android Studio so confused about things there.我是 Java 和 Android Studio 的初学者,所以对那里的事情感到困惑。 It would be great if you tell me how to make the app work in standard programming with fewer codes.如果您能告诉我如何使用更少的代码使该应用程序在标准编程中运行,那就太好了。 I mean if and editText is empty the textView won't show any data related to it.我的意思是,如果 editText 为空,textView 将不会显示任何与之相关的数据。

EG: If the user inputs only the name textView will show My name is name only.例:如果用户只输入姓名 textView 将显示My name is name only。 Or if the user inputs only the age textView will show I'm age years old only.或者,如果用户仅输入年龄 textView 将仅显示我的age Or if user gives both the textView will show both或者如果用户同时提供 textView 将显示两者

Probably this would be appropriate solution in Java (hopefully I did not code a typo, as I am typing directly in the SO pane):这可能是 Java 中的适当解决方案(希望我没有编写错字,因为我直接在 SO 窗格中键入):

StringBuilder sb = new StringBuilder();
if (!age.isEmpty()) {
    sb.append("My name is " + name);
}

if (!name.isEmpty()) {
  if (sb.length() != 0) {
    sb.append("\n");
  }
  sb.append("I'm " + age + " years old");
}

showData.setText(sb.toString());

By the way, if you were using Kotlin this would have been sooo much nicer.顺便说一下,如果您使用的是 Kotlin,那就更好了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从活动A中的EditText获取多个整数值以在活动B中的TextView中显示最小值和最大值? - How to get multiple integer values from EditText in Activity A to show min and max in TextView in Activity B? 如何在TextView和Android的EditText中显示来自API服务器的JSON数据? - How to display JSON data from API server in TextView and EditText in Android? 如何从LinearLayout中动态创建的EditText和TextView获取数据? - How to get data from dynamically created EditText and TextView inside a linearlayout? Android Studio 从 html 中获取数据并仅过滤一些值以在 textView 上显示 - Android studio get data from html and filter only some value to show on textView 如何从一个EditText获取多个变量? - How to get multiple variables from one EditText? 如何用一个以上的textview填充一个listview中的多个数据? - how to populate multiple data in a listview with more than one textview? 如何在 Textview 中显示数据 2 秒(android Studio)? - How do I show data in Textview for a 2 second (android Studio)? Android Studio - 如何从 RecyclerView 中的 EditText 获取数据并将它们放入主要活动中的 ArrayList? - Android Studio - How to get data from an EditText in a RecyclerView and put them into an ArrayList in the main activity? 如何将多个值从EditText设置为1 Textview并自动显示而无需单击按钮 - How to set multiple value from EditText to 1 Textview and Automaticaly show without click Button 如何在EditText中显示当前URL-Android Studio - How to show current url in EditText - android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM