如何获得编辑文本的标识在android的动态创建的编辑文本的点击? [英] How to get Id of edit text on click of dynamically created edit text in android?

查看:206
本文介绍了如何获得编辑文本的标识在android的动态创建的编辑文本的点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据项目要求在布局

我创建动态的看法。意见序列是从JSON。当我在编辑文字点击随即要求在编辑文本设定时间timepicker功能快到了,但是当我设置编辑文本值,那么它给出最后创建编辑文本的ID。
这里是我的code: -

 为(i = 0; I< alist_interface_type.size();我++){
                 如果(alist_interface_type.get㈠.equals(文本)){
                        的LinearLayout lLayout =新的LinearLayout(getActivity());
                        lLayout.setOrientation(LinearLayout.VERTICAL);
                        lLayout.setPadding(5,5,5,5);
                        lLayout.setBackgroundColor(Color.parseColor(#E0E0E0));
                        标签=新的TextView(getActivity());
                        label.setText(example.get(I));
                        label.setTextSize(16);
                        label.setTextColor((Color.parseColor(#01c534)));
                        label.setPadding(5,5,5,5);
                        等=新的EditText(getActivity());
                        et.setSingleLine(真);
                        et.setId(ⅰ);
                        et.setTextSize(18);
                        et.setPadding(5,5,5,5);
                        如果(!data_type.get(I)的ToString()。等于(文本)){
                            输入过滤器[] =过滤器新的输入过滤器[1];
                            过滤器[0] =新InputFilter.LengthFilter(的Integer.parseInt(data_type.get(ⅰ)的ToString()));
                            et.setFilters(过滤器);
                        }
                         LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT);
                        lLayout.addView(标签,LP);
                        lLayout.addView(ET,LP);
                        ll.addView(lLayout);
                        lp.setMargins(5,0,5,5);
                        lLayout.setLayoutParams(LP);
                        alist_id.add(ⅰ);
                        alist_views.add(EditText上);
                    }
                否则,如果(alist_interface_type.get(I).equals(时代){
                    的LinearLayout lLayout =新的LinearLayout(getActivity());
                    lLayout.setOrientation(LinearLayout.VERTICAL);
                    lLayout.setPadding(5,5,5,5);
                    lLayout.setBackgroundColor(Color.parseColor(#E0E0E0));
                    标签=新的TextView(getActivity());
                    label.setText(example.get(I));
                    label.setTextSize(16);
                    label.setTextColor((Color.parseColor(#01c534)));
                    label.setPadding(5,5,5,5);
                    ET2 =新的EditText(getActivity())
                    {
                        公共布尔getDefaultEditable(){
                            返回false;
                        }
                    };
                    et2.setSingleLine(真);
                    et2.setId(ⅰ);
                    et2.setTextSize(18);
                    Log.d(编辑文本cliked,+ I);
                    et2.setText(pref.getString(alist_Label_name.get(i)中,));
                    et2.setPadding(5,5,5,5);
                    如果(!data_type.get(I)的ToString()。等于(文本)){
                        输入过滤器[] =过滤器新的输入过滤器[1];
                        过滤器[0] =新InputFilter.LengthFilter(的Integer.parseInt(data_type.get(ⅰ)的ToString()));
                        et2.setFilters(过滤器);
                    }
                     LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.WRAP_CONTENT);
                    lLayout.addView(标签,LP);
                    lLayout.addView(ET2,LP);
                    lp.setMargins(5,0,5,5);
                    lLayout.setLayoutParams(LP);
                    ll.addView(lLayout);
                    alist_id.add(ⅰ);
                    alist_views.add(时代);
                    et2.setOnClickListener(新OnClickListener(){                        @覆盖
                        公共无效的onClick(视图v){
                            // TODO自动生成方法存根                            Log.d(编辑文字的ID,+ et2.getId());
                            timePicker();
                        }
                    });
                }                    }

在此code alist_interface_type是数组列表,包含我views.If这个列表包含时间,然后创建编辑文本,然后点击编辑文本通话时间选择器的方法选择时,并设置编辑text.I要赐给编辑文本编辑的点击text.On的ID我的价值它不会给特定的被点击编辑文本的id设置的值。


解决方案

  //尝试这种方式,希望这将帮助你...et2.setTag(et2.getId());
et2.setOnClickListener(新OnClickListener(){    @覆盖
    公共无效的onClick(最终查看V){
      // TODO自动生成方法存根      Log.d(编辑文字的ID,+ et2.getId());
      timePicker(新OnTimeSelected(){
           公共无效ONSELECT(字符串时间){
               ((EditText上)v.getTag())的setText(时间)。
           }
      });
    }
});
接口OnTimeSelected {
  公共无效ONSELECT(字符串时间);
}

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 .

解决方案

// 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);
}

这篇关于如何获得编辑文本的标识在android的动态创建的编辑文本的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆