android遍历界面上的所有控件后,如何判断控件是什么类型
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout loginLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
String pa="";
for (int i = 0; i < loginLayout.getChildCount(); i++) {
View v=loginLayout.getChildAt(i);
//如何判断是Button或者是TextBox
if(){
//就剩下这一步了,小弟百试不得成功!望高手解救哈!
}
}
}
} 可以通过它的类名来判断:v.getClassName() == "Button"
也可以通过instanceof判断:v instanceof Button
TableLayout myLayOut = (TableLayout)findViewById(R.id.tableLayout1);
for (int i = 0; i < myLayOut.getChildCount(); i++) {
View v = myLayOut.getChildAt(i);
if ( v instanceof ImageView){
ImageView myImageView = (ImageView)myLayOut.getChildAt(i);
myImageView.setOnClickListener(new myOnclickListener());
}
}