以编程方式从 EditText 请求数据

我正在创建一个以编程方式显示 UI 的类。我显示了很多 EditText 和其他内容,我想请求任何一个的数据保存在 BD 中。我怎样才能为其中任何一个提供一个 id?


这是代码


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

                String dato = lista.get(i).getData_type();

                String title = lista.get(i).getField_label();

                String id = lista.get(i).getField_name();



                if (dato.equals("Text")){

                    TextView tv = new TextView(getApplicationContext());

                    tv.setLayoutParams(lp);

                    Lview.addView(tv);

                    tv.setText("Ingrese "+title);

                    EditText et = new EditText(getApplicationContext());

                    et.setLayoutParams(lp);

                    et.setInputType(InputType.TYPE_CLASS_TEXT);

                    Lview.addView(et);

                    et.setHint("Ingrese Datos");

                }

            }


            btnIng.setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View view) {


                    Toast.makeText(plantilla.this, "El elemento es ", Toast.LENGTH_SHORT).show();

                }

            });


慕村9548890
浏览 104回答 1
1回答

慕少森

&nbsp;for (int i=0; i<lista.size();i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String dato = lista.get(i).getData_type();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String title = lista.get(i).getField_label();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String id = lista.get(i).getField_name();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dato.equals("Text")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView tv = new TextView(getApplicationContext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.setLayoutParams(lp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.setId(assignDifferentId)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lview.addView(tv);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tv.setText("Ingrese "+title);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EditText et = new EditText(getApplicationContext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; et.setLayoutParams(lp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;et.setId(assignDifferentId2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; et.setInputType(InputType.TYPE_CLASS_TEXT);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lview.addView(et);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; et.setHint("Ingrese Datos");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; btnIng.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(plantilla.this, "El elemento es ", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });您可以通过 id 访问它,如下所示EditText et = Lview.findViewById(yourassignedId)String text = et.getText().toString()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java