如何访问AlertBox和onClickListener之外的EditText引用

时间:2015-04-10 07:46:53

标签: android

我已经宣布了一个EditText数组" ed [] []"在onBoxListener中的alertBox中。我在同一个活动中的另一个onClickListener方法中使用了这个ed [] []引用,但它总是给出以下错误

ed无法解析为变量

这是我的代码:

private EditText nR,nC,nR1,nC1;
EditText[][] arr;
private Button submit,b2,bmul;
private LinearLayout matrix,matrix2;
int rows,cols,a1=1,b1=1,a3,b3;
private LinearLayout layout,layout2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dimensions);

        matrix = (LinearLayout) findViewById(R.id.matrix);
        matrix2 = (LinearLayout) findViewById(R.id.matrix2);
        matrix.setOrientation(LinearLayout.VERTICAL);
        matrix2.setOrientation(LinearLayout.VERTICAL);
        submit = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        bmul=(Button)findViewById(R.id.mul);
        submit.setOnClickListener(this);
        b2.setOnClickListener(this);

}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.button1:

       //creatDialog(matrix,submit);
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        LayoutInflater inflater = this.getLayoutInflater();


        alert.setTitle("Define Dimensions");
        alert.setMessage("Assign Numbers of Rows and Columns to Matrix");

        //Set an EditText view to get user input 

        final View tex =inflater.inflate(R.layout.dialog_dimension,null);

        alert.setView(tex);


        nR=(EditText)tex.findViewById(R.id.R2);
        nC=(EditText)tex.findViewById(R.id.C2);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            a1 = Integer.parseInt(nR.getText().toString());
            b1 = Integer.parseInt(nC.getText().toString());
            submit.setText(""+a1+"x"+b1);

            //multipleDimension(a1,b1,mat);

            **final EditText ed[][] = new EditText[a1][b1];**  
            matrix.removeAllViews();
            for (int i = 0; i < a1; i++) {
            layout = new LinearLayout(
                    Dimensions.this);

            layout.setOrientation(LinearLayout.HORIZONTAL);

            for (int j = 0; j < b1; j++) {

                ed[i][j] = new EditText(Dimensions.this);

                //ed[i][j].setId(+(i)+(j));

                ed[i][j].setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                ed[i][j].setHint("a"+(i+1)+(j+1));
                ed[i][j].setTextColor(Color.RED);



                layout.addView(ed[i][j]);
            }

            matrix.addView(layout);

            }


          }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();


    break;

    case R.id.button2:

        //creatDialog(matrix2,b2);

         //creatDialog(matrix,submit);
        AlertDialog.Builder alert1 = new AlertDialog.Builder(this);

        LayoutInflater inflater1 = this.getLayoutInflater();


        alert1.setTitle("Define Dimensions");
        alert1.setMessage("Assign Numbers of Rows and Columns to Matrix");

        //Set an EditText view to get user input 

        final View tex1 =inflater1.inflate(R.layout.dialog_dimension,null);

        alert1.setView(tex1);


        nR1=(EditText)tex1.findViewById(R.id.R2);
        nC1=(EditText)tex1.findViewById(R.id.C2);

        alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            a3 = Integer.parseInt(nR1.getText().toString());
            b3 = Integer.parseInt(nC1.getText().toString());
            b2.setText(""+a3+"x"+b3);

            //multipleDimension(a1,b1,mat);

            final EditText ed1[][] = new EditText[a3][b3];  
            matrix2.removeAllViews();
            for (int i = 0; i < a3; i++) {
            layout2 = new LinearLayout(
                    Dimensions.this);

            layout2.setOrientation(LinearLayout.HORIZONTAL);

            for (int j = 0; j < b3; j++) {

                ed1[i][j] = new EditText(Dimensions.this);

                //ed[i][j].setId(+(i)+(j));

                ed1[i][j].setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                ed1[i][j].setHint("a"+(i+1)+(j+1));
                ed1[i][j].setTextColor(Color.RED);



                layout2.addView(ed1[i][j]);
            }

            matrix2.addView(layout2);

            }

          }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();



    break;
}
    bmul.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(Dimensions.this,ArithmaticOperation.class);
            String s[][]= new String[a1][b1];
            intent.putExtra("m", a1);
            intent.putExtra("n", b1);
            for (int i=0;i<a1; i++){
                for (int j=0;j<a1; j++){

                    s[i][j]=**ed[i][j]**.getText().toString();
                    intent.putExtra("key["+i+"]["+j+"]",  s[i][j]);

                }

            }
        }
    });
}




        }

这里我实例化EditText数组

case R.id.button1:

       //creatDialog(matrix,submit);
        AlertDialog.Builder alert = new AlertDialog.Builder(this);

        LayoutInflater inflater = this.getLayoutInflater();


        alert.setTitle("Define Dimensions");
        alert.setMessage("Assign Numbers of Rows and Columns to Matrix");

        //Set an EditText view to get user input 

        final View tex =inflater.inflate(R.layout.dialog_dimension,null);

        alert.setView(tex);


        nR=(EditText)tex.findViewById(R.id.R2);
        nC=(EditText)tex.findViewById(R.id.C2);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            a1 = Integer.parseInt(nR.getText().toString());
            b1 = Integer.parseInt(nC.getText().toString());
            submit.setText(""+a1+"x"+b1);

            //multipleDimension(a1,b1,mat);

            **final EditText ed[][] = new EditText[a1][b1];**  
            matrix.removeAllViews();
            for (int i = 0; i < a1; i++) {
            layout = new LinearLayout(
                    Dimensions.this);

            layout.setOrientation(LinearLayout.HORIZONTAL);

            for (int j = 0; j < b1; j++) {

                ed[i][j] = new EditText(Dimensions.this);

                //ed[i][j].setId(+(i)+(j));

                ed[i][j].setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                ed[i][j].setHint("a"+(i+1)+(j+1));
                ed[i][j].setTextColor(Color.RED);



                layout.addView(ed[i][j]);
            }

            matrix.addView(layout);

            }


          }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();


    break;

在这里我得到了错误

bmul.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(Dimensions.this,ArithmaticOperation.class);
            String s[][]= new String[a1][b1];
            intent.putExtra("m", a1);
            intent.putExtra("n", b1);
            for (int i=0;i<a1; i++){
                for (int j=0;j<a1; j++){

                    s[i][j]=**ed[i][j]**.getText().toString();
                    intent.putExtra("key["+i+"]["+j+"]",  s[i][j]);

                }

            }
        }
    });

1 个答案:

答案 0 :(得分:0)

private EditText nR, nC, nR1, nC1;
EditText[][] arr;
private Button submit, b2, bmul;
private LinearLayout matrix, matrix2;
int rows, cols, a1 = 1, b1 = 1, a3, b3;
private LinearLayout layout, layout2;
final EditText ed[][] = new EditText[a1][b1];
final EditText ed1[][] = new EditText[a3][b3];

eded1的声明移到班级的顶部。