How to get Id of edit text on click of dynamically

2019-03-04 01:04发布

问题:

I am creating dynamically views in layout according to project requirements. Sequence of views are coming from json .when i click on edit text then timepicker function called for setting time in edit text ,but when i set the edit text value then its gives the id of last created edit text. here is my code:-

for (i = 0; i < alist_interface_type.size(); i++) {
                 if(alist_interface_type.get(i).equals("textbox")) {
                        LinearLayout lLayout = new LinearLayout(getActivity());
                        lLayout.setOrientation(LinearLayout.VERTICAL);
                        lLayout.setPadding(5, 5, 5, 5);
                        lLayout.setBackgroundColor(Color.parseColor("#E0E0E0"));
                        label = new TextView(getActivity());
                        label.setText(example.get(i));
                        label.setTextSize(16);
                        label.setTextColor((Color.parseColor("#01c534")));
                        label.setPadding(5, 5, 5, 5);
                        et = new EditText(getActivity());
                        et.setSingleLine(true);
                        et.setId(i);
                        et.setTextSize(18);
                        et.setPadding(5, 5, 5, 5);
                        if (!data_type.get(i).toString().equals("text")) {
                            InputFilter[] filters = new InputFilter[1];
                            filters[0] = new InputFilter.LengthFilter(Integer.parseInt(data_type.get(i).toString())); 
                            et.setFilters(filters);
                        }
                         LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT);
                        lLayout.addView(label, lp);
                        lLayout.addView(et, lp);
                        ll.addView(lLayout);
                        lp.setMargins(5, 0, 5, 5);
                        lLayout.setLayoutParams(lp);
                        alist_id.add(i);
                        alist_views.add("EditText");
                    }
                else if (alist_interface_type.get(i).equals("time") {
                    LinearLayout lLayout = new LinearLayout(getActivity());
                    lLayout.setOrientation(LinearLayout.VERTICAL);
                    lLayout.setPadding(5, 5, 5, 5);
                    lLayout.setBackgroundColor(Color.parseColor("#E0E0E0"));
                    label = new TextView(getActivity());
                    label.setText(example.get(i));
                    label.setTextSize(16);
                    label.setTextColor((Color.parseColor("#01c534")));
                    label.setPadding(5, 5, 5, 5);
                    et2 = new EditText(getActivity())
                    {
                        public boolean getDefaultEditable() {
                            return false;
                        }
                    };
                    et2.setSingleLine(true);
                    et2.setId(i);
                    et2.setTextSize(18);
                    Log.d("edit text cliked", ""+i);
                    et2.setText(pref.getString(alist_Label_name.get(i),""));
                    et2.setPadding(5, 5, 5, 5);
                    if (!data_type.get(i).toString().equals("text")) {
                        InputFilter[] filters = new InputFilter[1];
                        filters[0] = new InputFilter.LengthFilter(Integer.parseInt(data_type.get(i).toString())); 
                        et2.setFilters(filters);
                    }
                     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.WRAP_CONTENT);
                    lLayout.addView(label, lp);
                    lLayout.addView(et2, lp);
                    lp.setMargins(5, 0, 5, 5);
                    lLayout.setLayoutParams(lp);
                    ll.addView(lLayout);
                    alist_id.add(i);
                    alist_views.add("time");
                    et2.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub

                            Log.d("id of edit text", ""+et2.getId());
                            timePicker();
                        }
                    });
                }

                    }

In this code alist_interface_type is arraylist that containing my views.If this list contains "time" then create edit text and click on edit text call time picker method for selecting time and set to edit text.I am giving the value of i to id of edit text.On click of edit text it doesn't give the id of particular clicked edit text to set the value .

回答1:

// try this way,hope this will help you...

et2.setTag(et2.getId());
et2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(final View v) {
      // TODO Auto-generated method stub

      Log.d("id of edit text", ""+et2.getId());
      timePicker(new OnTimeSelected(){
           public void onSelect(String time){
               ((EditText)v.getTag()).setText(time);
           }
      });
    }
});


interface OnTimeSelected{
  public void onSelect(String time);
}