java - 在 Android 中管理多个 EditText

标签 java android android-edittext

Possible Duplicate:
android: how to elegantly set many button IDs

大家。

我正在尝试构建我的第一个 Android 应用程序来进行一些 RMS(均方根)误差计算。为此,我需要几个实验点输入,这是在 EditTexts 中手动完成的。所以我在一个布局中有 40 个 EditText。问题是在管理它们时,我有这样的事情:

public class InsertarData extends Activity implements OnClickListener {
EditText x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17;
EditText y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13,y14,y15,y16,y17;
float [] datosX;
float [] datosY;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.insertardatos);
    x1=(EditText)findViewById(R.id.x1Et);
    x2=(EditText)findViewById(R.id.x2Et);
    x3=(EditText)findViewById(R.id.x3Et);
    x4=(EditText)findViewById(R.id.x4Et);
    x5=(EditText)findViewById(R.id.x5Et);
    x6=(EditText)findViewById(R.id.x6Et);
    x7=(EditText)findViewById(R.id.x7Et);
    x8=(EditText)findViewById(R.id.x8Et);
    x9=(EditText)findViewById(R.id.x9Et);
    x10=(EditText)findViewById(R.id.x10Et);
    x11=(EditText)findViewById(R.id.x11Et);
    x12=(EditText)findViewById(R.id.x12Et);
    x13=(EditText)findViewById(R.id.x13Et);
    x14=(EditText)findViewById(R.id.x14Et);
    x15=(EditText)findViewById(R.id.x15Et);
    x16=(EditText)findViewById(R.id.x17Et);
    x17=(EditText)findViewById(R.id.x17Et);

    y1=(EditText)findViewById(R.id.y1Et);
    y2=(EditText)findViewById(R.id.y2Et);
    y3=(EditText)findViewById(R.id.y3Et);
    y4=(EditText)findViewById(R.id.y4Et);
    y5=(EditText)findViewById(R.id.y5Et);
    y6=(EditText)findViewById(R.id.y6Et);
    y7=(EditText)findViewById(R.id.y7Et);
    y8=(EditText)findViewById(R.id.y8Et);
    y9=(EditText)findViewById(R.id.y9Et);
   y10=(EditText)findViewById(R.id.y10Et);
   y11=(EditText)findViewById(R.id.y11Et);
   y12=(EditText)findViewById(R.id.y12Et);
   y13=(EditText)findViewById(R.id.y13Et);
   y14=(EditText)findViewById(R.id.y14Et);
   y15=(EditText)findViewById(R.id.y15Et);
   y16=(EditText)findViewById(R.id.y16Et);
   y17=(EditText)findViewById(R.id.y17Et);

   Button continuar=(Button)findViewById(R.id.continuarBtn);
   continuar.setOnClickListener(this);

}

@Override
public void onClick(View e) {
    switch(e.getId()){
        case R.id.continuarBtn:{
        datosX=new float[17];
        datosX[0]=Float.parseFloat(x1.getText().toString());
        datosX[1]=Float.parseFloat(x2.getText().toString());
        datosX[2]=Float.parseFloat(x3.getText().toString());
        datosX[3]=Float.parseFloat(x4.getText().toString());
        datosX[4]=Float.parseFloat(x5.getText().toString());
        datosX[5]=Float.parseFloat(x6.getText().toString());
        datosX[6]=Float.parseFloat(x7.getText().toString());
        datosX[7]=Float.parseFloat(x8.getText().toString());
        datosX[8]=Float.parseFloat(x9.getText().toString());
        datosX[9]=Float.parseFloat(x10.getText().toString());
        datosX[10]=Float.parseFloat(x11.getText().toString());
        datosX[11]=Float.parseFloat(x12.getText().toString());
        datosX[12]=Float.parseFloat(x13.getText().toString());
        datosX[13]=Float.parseFloat(x14.getText().toString());
        datosX[14]=Float.parseFloat(x15.getText().toString());
        datosX[15]=Float.parseFloat(x16.getText().toString());
        datosX[16]=Float.parseFloat(x17.getText().toString());

        datosY[0]=Float.parseFloat(y1.getText().toString());
        datosY[1]=Float.parseFloat(y2.getText().toString());
        datosY[2]=Float.parseFloat(y3.getText().toString());
        datosY[3]=Float.parseFloat(y4.getText().toString());
        datosY[4]=Float.parseFloat(y5.getText().toString());
        datosY[5]=Float.parseFloat(y6.getText().toString());
        datosY[6]=Float.parseFloat(y7.getText().toString());
        datosY[7]=Float.parseFloat(y8.getText().toString());
        datosY[8]=Float.parseFloat(y9.getText().toString());
        datosY[9]=Float.parseFloat(y10.getText().toString());
        datosY[10]=Float.parseFloat(y11.getText().toString());
        datosY[11]=Float.parseFloat(y12.getText().toString());
        datosY[12]=Float.parseFloat(y13.getText().toString());
        datosY[13]=Float.parseFloat(y14.getText().toString());
        datosY[14]=Float.parseFloat(y15.getText().toString());
        datosY[15]=Float.parseFloat(y16.getText().toString());
        datosY[16]=Float.parseFloat(y17.getText().toString());

        }
        }
    }

}

这个“有效”(需要将 datosX 和 datosY 传递给另一个 Activity 来完成。但是,在此之前,我需要知道 EditTexts 是否为空,我无法想象做类似 if(edittext.getText( ).toString()!=null) 在每个 parseFloat 之前。

是否有更好的方法来管理大量 EditText 并知道它们是否为空?我想过一个 EditText 数组并执行类似的操作:

EditText editarray[17];
editarray[0]=(EditText)findViewById(R.id.edittext0);

这是这样做的方法吗?

非常感谢您提前阅读并回答。对不起我的英语。

最佳答案

像这样怎么样?

EditText xE, yE;

float x, y;

int[] xIds = { R.id.x1et, R.id.x2et, /*...etc*/ };
int[] yIds = { R.id.y1et, R.id.y2et, /*...etc*/ };

for(int i = 0; i < xIds.length && i < yIds.length; i++) {
    xE = (EditText)findViewById(xIds[i]);
    yE = (EditText)findViewById(yIds[i]);
    try { 
        x = Float.parseFloat(xE.getText().toString());
        y = Float.parseFloat(yE.getText().toString());
        //do whatever you need with the data here
    } catch (NumberFormatException e) {
        //Either there was an invalid input, or field was empty
        //You can add extra logic here to check them for empty fields
        //or just exit the loop here
    }
}

关于java - 在 Android 中管理多个 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7698574/

相关文章:

java - 为什么在传递类型为参数和返回类型的 lambda 表达式时,java 将类型参数设置为对象?

android - 在不重新启动应用程序的情况下转移 Android 应用程序开发框架

android - Gradle 同步错误未能找到 : com. google.android.gms :play-services:6. 1.+'

android - 在没有扩展类 Activity 的情况下,如何从联系人那里获取电子邮件 ID 并从联系人那里获取电话号码?

android - EditText 中的双光标用于输入类型数字/电话(RTL 阿拉伯语)

android - 无法在某些设备中膨胀 EditText

android - 自动调整 EditText

java - 如何在 sonar build.xml 中仅包含单个文件

java - Tomcat - 有没有办法吐出尚未响应的当前/Activity 请求?

java - 使用java运行终端命令来清除终端历史记录