开发者

Get ID for views that are added dynamically

开发者 https://www.devze.com 2023-03-21 02:26 出处:网络
How to get ID of Views that are added dynamically in layout? public class Display { static int i; Dynamic_UIActivity dyn;

How to get ID of Views that are added dynamically in layout?

   public class Display 
    {

        static int i;
        Dynamic_UIActivity dyn;
        private Context context;
        View mField= null;
        TextView text;
        protected int mYear;
        protected int mMonth;
        protected int mDay;
        LinearLayout ll;
        Mynewdialog dialognew;

        public Display(Context mcontext)
        {
            context=mcontext;
            ll = new LinearLayout(mcontext);
            ll.setOrientation(LinearLayout.VERTICAL);
            dyn = new Dynamic_UIActivity();
       }

       public void addLabel(String text)
       {
        TextView textview = new TextView(context);
        textview.setText(text);
        ll.addView(textview);       
        }

        public void addField(String Type,String Tag)
        {
            mField.setId(i);
            i++;
            if(Type.equalsIgnoreCase("text"))
           {
                mField= new EditText(context);
                mField.setTag(Tag);
                //int id = mField.getResources(开发者_JS百科).getIdentifier((String) mField.getTag(),"id" ,dyn.getPackageName());
                //EditText edit = (EditText)mField.findViewById(1);
                ll.addView(mField);
            }
            if(Type.equalsIgnoreCase("datepicker"))
            {
                mField=new EditText(context);
                ((EditText) mField).setText("Pick Date");
                ll.addView(mField);
                mField.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v) 
                    {
                        dialognew = new Mynewdialog(context);
                         dialognew.dialog_obj((EditText) mField);
                    }
                });
            } 
        }   

        protected DatePickerDialogWithEditText.OnDateSetListener mdatepickerdialog = new DatePickerDialogWithEditText.OnDateSetListener()
        {
            public void onDateSet(DatePicker view , int year , int month, int date)
             {
                  mYear = year;
                  mMonth = month;
                 mDay = date;
                 dialognew.showdialogtext();

             }
         };
     }

     class DatePickerDialogWithEditText extends DatePickerDialog 
     {
         EditText tv;    

         DatePickerDialogWithEditText(Context c, OnDateSetListener oD, int y, int m, int d, EditText tv) 
        {
             super(c, oD, y, m, d);
             this.tv = tv;
        }

         EditText getAssociatedTextView() 
         {
             return tv;
         }
    }


      class Mynewdialog 
     {
        private Context context;
        Display dis = new Display(context);
        private DatePickerDialogWithEditText dialog;

        public Mynewdialog(Context mcontext)
        {
            context=mcontext;       
        }

        public void dialog_obj(EditText editTextView)
        {
             final Calendar cal = Calendar.getInstance();
             dis.mYear = cal.get(Calendar.YEAR);
             dis.mMonth = cal.get(Calendar.MONTH);
             dis.mDay = cal.get(Calendar.DAY_OF_MONTH);
             dialog = new DatePickerDialogWithEditText(context, dis.mdatepickerdialog, dis.mYear, dis.mMonth, dis.mDay, editTextView);
            dialog.show();
        }

         public void showdialogtext()
        {
             EditText et = dialog.getAssociatedTextView();
             et.setText(new StringBuilder().append(dis.mDay).append("-").append(dis.mMonth).append("-").append(dis.mYear));
         }  
       }

now I want to Set the ID of the views that I get by checking its Type?how can I do that?


You can do it by using getId() and setId() methods of the View.

For example:

TextView txtView = new TextView(this);
txtView.setId(4);

and you can get the ID by using getId() method:

txtView.getId();


or you can add all(Dynamically created) views in a ArrayList and then access it.

0

精彩评论

暂无评论...
验证码 换一张
取 消