Android如何获取动态添加控件的id:
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.relaGameZhaiGuoZi);
imgApple2 = new ImageView(this);
imgApple2.setImageResource(R.drawable.app);
imgApple2.setId(110);
imgApple2.setOnClickListener(this);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp1.leftMargin=30;
lp1.topMargin = 100;
mainLayout.addView(imgApple2,lp1);
View类里面有一个findFocus的成员函数,作用相当于找到以当前View为根的view层次结构中,获得当前输入焦点的view 。
1、rootview.findFocus().getId()
2、代码如下
View rootview = this.getWindow().getDecorView();
int aaa = rootview.findFocus().getId();
Log.i("",aaa);
根控件知道吧?rootview.findFocus().getId()