【问题标题】:Get parent view class of view instance获取视图实例的父视图类
【发布时间】:2013-07-19 13:26:43
【问题描述】:

如果我的 View 是 Button 的实例,我需要一个方法来获取信息。

MyButton 是 Button 的子类。

public void onCreate(Bundle s) 
{
    ...
    MyButton button = new MyButton(activity);
    getViewType(button);
}

private <V extends View> V getViewClass(V view) 
{
    Class<V> clazz = (Class<T>) view.getClass();
    if (clazz instanceof Button) {
        return Button.class; //the information I need to get
    }
}

instanceof 在这里不起作用。 我可以将 clazz 与 Classes 进行比较,如下所示。但是,如果此视图实例是 Button 类的子级,我需要这些信息。

if (clazz == Button.class) ... //returns false
if (clazz == MyButton.class) ... //returns true

编辑:

我明白了。解决方案:

if (Button.class.isAssignableFrom(clazz)) 
{ 
    ... 
}

【问题讨论】:

  • 如果您找到答案,请将其添加为答案并接受,因此问题将被关闭。

标签: android class view parent instanceof


【解决方案1】:

为什么不这样做呢?

private <V extends View> V getViewClass(V view) 
{        
    if (view instanceof Button) {
        return Button.class; //the information I need to get
    }
}

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 2014-02-04
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多