Creating control array in Android

2019-06-14 04:30发布

How to create a control array in Android? How to create a multidimensional (2D) array in Android?

for buttons, edit text

also how can we access the elements of edit text in a 2 dimensional array

How are multidimensional arrays created in Android? And in particular a 2 dimensional array to contain Buttons or EditText?

Also, how does one access elements of these arrays once they have been created?

1条回答
你好瞎i
2楼-- · 2019-06-14 05:01

Look at this sample what I created:

Button[] buttons = new Button[10];
EditText[] textboxes = new EditText[10];

View[][] buttonsAndEditboxes = new View[2][5];

// adds 10 buttons to one-dimensional array
for (int i = 0; i < 10; i++) {
   buttons[i] = new Button(this);
}

// adds 10 editexts to one-dimensional array
for (int i = 0; i < 10; i++) {
   textboxes[i] = new EditText(this);
}

for (int i = 0; i < 2; i++) {
   for (int j = 0; j < 5; j++) {
      if (i == 0) {
         // adds 5 buttons to 2D array
         buttonsAndEditboxes[i][j] = new Button(this);
      } else {
         // adds 5 edittexts to 2D array
         buttonsAndEditboxes[i][j] = new EditText(this);
      }
   }
}

// access first button from the one-dimensional array           
Button fromOneDimensionalButton = buttons[0];

// access first EditText from the one-dimensional array
EditText fromOneDimensionalEdit = textboxes[0];

// access Button from the two-dimensional array
Button fromTwoDimensionalButton = (Button) buttonsAndEditboxes[0][0];

// access EditText from the two-dimensional array
EditText fromTwoDimensionalEditText = (EditText) buttonsAndEditboxes[1][0];

EDIT:

Add some code as an example for you:

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/test" >
</LinearLayout>

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layout = (LinearLayout) findViewById(R.id.test);

        View[][] buttonsAndEditboxes = new View[2][5];

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 5; j++) {
                if (i == 0) {
                    // adds 5 buttons to 2D array
                    buttonsAndEditboxes[i][j] = new Button(this);
                } else {
                    // adds 5 edittexts to 2D array
                    buttonsAndEditboxes[i][j] = new EditText(this);
                    buttonsAndEditboxes[i][j].setOnClickListener(this);

                    // you need to create unique id here
                    buttonsAndEditboxes[i][j].setId(j);
                    layout.addView(buttonsAndEditboxes[i][j]);

                }
            }
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case 0:
            Toast.makeText(this, "Editext0 clicked", Toast.LENGTH_SHORT).show();
            break;

        case 1:
            Toast.makeText(this, "Editext1 clicked", Toast.LENGTH_SHORT).show();
            break;

        case 2:
            Toast.makeText(this, "Editext2 clicked", Toast.LENGTH_SHORT).show();
            break;

        default:
            break;
        }
    }
}
查看更多
登录 后发表回答