【问题标题】:Get information checkbox and EditText within a LinearLayout在 LinearLayout 中获取信息复选框和 EditText
【发布时间】:2015-07-07 21:14:55
【问题描述】:

我有一个由查询动态生成的复选框和 EditText

这是生成复选框和EditText并将其添加到LinearLayout的方法

private void create_form() {
    JSONObject json = null;
    int count = 0;
    lm = (LinearLayout) findViewById(R.id.linearMain);
    int seccion = Integer.parseInt(conf.get_id_seccion());
    int tipo_solicitud = Integer.parseInt(conf.get_tipo_solicitud());
    JSONObject jobj = obj_sqlite.get_form(seccion, tipo_solicitud);


    try {
        count = Integer.parseInt(jobj.getString("cont"));
        json = new JSONObject(jobj.getString("json"));
    } catch (Exception e) {
        Log.e("getParams", e.getMessage());
    }

    for (int x = 1; x <= count; x++) {

        try {
            JSONObject json_row = new JSONObject(json.getString("row" + x));

            CheckBox chb = new CheckBox(this);
            chb.setText(json_row.getString("pregunta"));
            chb.setId(json_row.getInt("pregunta_verificacion_supervision"));
            chb.setTextSize(10);
            chb.setPadding(8, 3, 8, 3);
            chb.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
            chb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));

            lm.addView(chb);

            EditText et = new EditText(this);
            et.setHint("observaciones");
            et.setId(json_row.getInt("pregunta_verificacion_supervision"));
            et.setTextSize(10);
            et.setPadding(8, 3, 8, 3);
            et.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
            et.setInputType(android.text.InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
            et.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));

            lm.addView(et);


        } catch (Exception e) {
            Log.e("getParams", e.getMessage());
        }
    }
}

现在我需要选中所有这些复选框以及您的 EditText 以将它们保留在表格中

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.php_mysql_sqlite.Formulario_verificacion_supervision" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/title_activity_preguntas_revision" android:id="@+id/textView1"/>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="285dp"
    android:layout_height="330dp"
    android:layout_marginTop="30dp" >

    <LinearLayout
        android:id="@+id/linearMain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:background="@color/White"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

<Button
    android:id="@+id/bt_regresar"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/scrollView1"
    android:onClick="regresar"
    android:text="@string/bt_regresar" />

<Button
    android:id="@+id/bt_guardar"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/bt_finalizar"
    android:layout_alignBottom="@+id/bt_finalizar"
    android:layout_alignLeft="@+id/scrollView1"
    android:text="@string/bt_guardar"
    android:onClick="guardar" />

<Button
    android:id="@+id/bt_finalizar"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/bt_regresar"
    android:layout_alignBottom="@+id/bt_regresar"
    android:layout_marginLeft="31dp"
    android:layout_toRightOf="@+id/bt_guardar"
    android:text="@string/bt_finalizar"
    android:onClick="finalizar" />

这是目前的一张图片 http://i61.tinypic.com/2uo2hi8.jpg

通过在按钮单击时调用的方法,进行获取数据的操作

谢谢大家

PS:如果你给我负分,请留下评论,因为它过去发生在我身上,不能做错

【问题讨论】:

    标签: android android-layout checkbox


    【解决方案1】:

    你有两个选择。

    • 为每个复选框和 EditText 设置一个唯一的 id,并将它们的 id 保存在 ArrayList 中。当您要进行检查时

      for(int i = 0 ; i < listOfCboxIds.size() ; ++i) {
          CheckBox cbox = (CheckBox) findViewById(listOfCboxIds.get(i));
          if(cbox.isChecked()) {
              // Do something
          }
      }
      
    • 将 CheckBoxes 和 EditTexts 的引用保存在 ArrayList 中并对其进行迭代。这占用的 CPU 资源较少。

      List<CheckBox> yourCheckBoxes = new ArrayList<CheckBox>();
      List<EditText> yourEditTexts  = new ArrayList<EditText>();
      
      
        try {
                  JSONObject json_row = new JSONObject(json.getString("row" + x));
      
                  CheckBox chb = new CheckBox(this);
                  chb.setText(json_row.getString("pregunta"));
                  chb.setId(json_row.getInt("pregunta_verificacion_supervision"));
                  chb.setTextSize(10);
                  chb.setPadding(8, 3, 8, 3);
                  chb.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
                  chb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                          LayoutParams.WRAP_CONTENT));
      
                  lm.addView(chb);
      
                  EditText et = new EditText(this);
                  et.setHint("observaciones");
                  et.setId(json_row.getInt("pregunta_verificacion_supervision"));
                  et.setTextSize(10);
                  et.setPadding(8, 3, 8, 3);
                  et.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC);
                  et.setInputTyp
      

      e(android.text.InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE); et.setLayoutParams(新 LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

              lm.addView(et);
              yourCheckBoxes.add(chb);
              yourEditTexts.add(et);
      
      
          } catch (Exception e) {
              Log.e("getParams", e.getMessage());
          }
      

    一旦你需要它们

    for(int i = 0 ; i < yourCheckBoxes.size() ; ++i) {
        CheckBox cbox = (CheckBox) yourCheckBoxes.get(i);
        EditText et = (EditText) yourEditTexts.get(i);
        if(cbox.isChecked()) {
            // Do something
        }
    }
    

    【讨论】:

      【解决方案2】:

      就像 Bojan Kseneman 所说,最好的选择是将您的视图存储在数据结构中。这可能如下所示:

      List<CheckBox> checkBoxes = new ArrayList<>();
      List<EditText> editTexts = new ArrayList<>();
      

      然后将每个框和 editText 添加到这些列表中,然后像这样迭代它们:

      for(int i = 0; i < checkBoxes.size(); i++){
          CheckBox box = checkBoxes.get(i);
          EditText editText = editTexts.get(i);
      
          doSomeThingWithThem();
      }
      

      希望这会有所帮助。

      【讨论】:

      • 需要在表格中保留选中复选框以及 EditText @RubyDerBoos 的值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2016-06-03
      • 2022-08-14
      • 1970-01-01
      • 2014-07-12
      相关资源
      最近更新 更多