简体   繁体   中英

Problem detecting Spinner component

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 the other two ?


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

Thanks

我的错误......我之前检查过它是否是一个ViewGroup对象所以它从未达到过这个条件

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

edit:

I found Stackoverflow question that explain it:

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

Have you considered using

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM