Managing several EditTexts in Android [duplicate]
Possible Duplicate:
android: how to elegantly set many button IDs
Everyone.
I'm trying to build my fir开发者_如何学Gost Android app for doing some RMS (Root Mean Square) error calculation. For this I need several experimental points inputs, this is done manually in EditTexts. So I have 40 EditTexts in one layout. The problem is when it comes to managing them, I have something like this:
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());
}
}
}
}
This "works" (it needs to be completed passing the datosX and datosY to another activity. But, before, I need to know whether the EditTexts are empty or not and I cannot imagine doing something like if(edittext.getText().toString()!=null) before every parseFloat.
Is there any better way to manage a lot of EditTexts and know if they're empty or not? I've though of an array of EditText and doing something like:
EditText editarray[17];
editarray[0]=(EditText)findViewById(R.id.edittext0);
Is this the way of doing it?
Thank you very much for reading and answering in advance. Sorry for my English.
How about something like this?
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
}
}
Are these EditText controls below each other? If so, I'd use a ListView, and have a separate row for every EditText. Then, it'd be easier to handle this many EditTexts in the list's adapter. It'd also require less memory, since the ListView is virtualized. But I guess the UI would be just terrible to use.
you can get the id of the view by using resources, it becomes easier to manage multiple views like this. yes, array seems is a good solution to manage the edit texts like this.
to access your 1st editText, assuming you gave the id of the editText as edittext0
then you can get access to it by.
int idsArray[40];
for(int i=0; i<40; i++){
String textString = "edittext" + Integer.toString(i);
int editId = getResources.getIdentifier(textString, "id", getPackageName());
// store the id's in an array
idsArray[i] = editId;
}
you can use the same method to get access to your editText.
EditText et = (EditText) findViewById(idsArray[i]);
to get access to each edit text in your layout and then check if the editText has data.
HTH.
You can also iterate over layout's children and determine which of them are EditText
Layout with your EditText widgets:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Iteration:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)findViewById(R.id.layout);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
Log.i("tag", child.getClass().getCanonicalName());
}
}
精彩评论