android – 获取动态添加的视图的ID

前端之家收集整理的这篇文章主要介绍了android – 获取动态添加的视图的ID前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何获取布局中动态添加的视图的ID?
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().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));
         }  
       }

现在我想通过检查其类型来设置我获得的视图的ID?我该怎么做?

解决方法

您可以使用View的getId()和setId()方法来完成此操作.

例如:

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

并且您可以使用getId()方法获取ID:

txtView.getId();
原文链接:/android/315773.html

猜你在找的Android相关文章