开发者

Problem detecting Spinner component

开发者 https://www.devze.com 2023-02-19 01:45 出处:网络
iterating through a View\'s components the following code works: if (child.getClass() == EditText.class) {

iterating through a View's components the following code works:

if (child.getClass() == EditText.class) {
  ...
} else if (child.getClass() == TextView.class) {
  ...

but this doesn't:

} else if (child.getClass() == Spinner.class) {
  ...

What's the difference between the Spinner class an th开发者_开发技巧e other two ?


My mistake ... I was previousy checking if it was a ViewGroup object so It never reached the condition

Thanks


My mistake ... I was previousy checking if it was a ViewGroup object so It never reached the condition


Have you considered using

if(child instanceof EditText){}
else if(child instanceof TextView){}
else if(child instanceof Spinner){}


 if(child.getClass() instanceof Spinner.class){
 ...

edit:

I found Stackoverflow question that explain it:

Any reason to prefer getClass() over instanceof when generating .equals()?

0

精彩评论

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