可能重复:
android: how to elegantly set many button IDs管理几个EditTexts Android中
大家。
我试图建立我的第一个Android应用程序来做一些RMS(均方根)误差计算。为此,我需要多个实验点输入,这是在EditTexts中手动完成的。所以我在一个布局中有40个EditTexts。问题是当它涉及到管理他们,我有这样的事情:
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到另一个活动要完成,但在此之前,我需要知道的。无论EditTexts是否为空,我都无法想象在每个parseFloat之前执行类似if(edittext.getText()。toString()!= null)的操作。
是否有更好的方法来管理大量EditText并知道如果它们是空的或没有?我虽然有一组EditText并做类似的事情:
EditText editarray[17];
editarray[0]=(EditText)findViewById(R.id.edittext0);
这是这样做吗?
非常感谢您的阅读和提前回答。对不起我的英语不好。
我想我会这样做,它只是我需要和想要做的事情,但我不知道如何设置阵列。 你们真棒,谢谢! – danielrozo