如何从编程创建的Edittext获取id值

时间:2015-04-02 04:42:21

标签: android android-edittext

    calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mul=0;
            sum=0;

            for(j=0;j<=a-1;j++){

                Log.d("TAG","a ko value inside calc "+a);

                et_grade_grabber=(EditText) findViewById(grade[j]);
                int grade= Integer.parseInt(et_grade_grabber.getText().toString());

                Log.d("TAG","Grade Value of Grade"+j+ " is "+grade);

                et_credit_grabber=(EditText) findViewById(credit[j]);
                int credit=Integer.parseInt(et_credit_grabber.getText().toString());
                Log.d("TAG","Credit Value of Credit "+j+" is "+credit);

                tot_credit= credit+tot_credit;
                Log.d("TAG","Total Credit = "+tot_credit);

                mul=credit*grade;

                sum= sum + mul;
                Log.d("Sum Inside Loop ",""+sum);
            }

            Log.d("TAG","Sum"+sum);

            Toast.makeText(getApplicationContext(),""+sum,Toast.LENGTH_SHORT).show();
            sgpa= sum/tot_credit;
            tv_sgpa= new TextView(MainActivity.this);
            tv_sgpa.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT));
            tv_sgpa.setText("Your SGPA is "+sgpa);
            tv_sgpa.setTextSize(40);


            LinearLayout ll_sgpa = new LinearLayout(MainActivity.this);
            ll_sgpa.setLayoutParams(new ActionBar.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));

            ll_sgpa.addView(tv_sgpa);

            LinearLayout linear = (LinearLayout) findViewById(R.id.ll_spga);
            linear.addView(ll_sgpa);
        }
    });
}

我已经使用java文件中的for(i = 0; i&lt; 4-1; i ++)循环成功创建了EditText字段,并在循环内通过setid(Array [i])分配id。

现在我再次通过方法getText()。toString()在循环内为(j = 0; j&lt; 4-1; j ++)

回溯值

当我在编辑文本中输入值时,一切正常,但它只检索编辑文本的第一个值到所有数组。

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你应该使用

for(int i = 0; i < arrayOfEditTexts.size(); i++){

    //if you need only some of Ids, just check conditions with `if`statement. example:
    if(!((EditText)arrayOfEditTexts.get(i)).getText().toString().equalsIgnoreCase("")){
         arrayOfEditTexts.get(i).getId(); // and do whatever you want
    }
}