简体   繁体   English

通过 Class 名称作为属性 XML 在 Android

[英]pass Class name as attribute XML in Android

I have classes which extend one class我有扩展一个 class 的类

public abstract class FatherClass{
    int num;
    FatherClass(int num){
        this.num= num;
    }   
}

public class FirstSonClass extend FatherClass{
    FirstSonClass(int num){
        super(num)
    }
}

public class SecondSonClass extend FatherClass{
    SecondSonClass(int num){
        super(num)
    }
}

when my program starts it should create an static instance of one of the sons.当我的程序启动时,它应该创建一个儿子的 static 实例。

public class MainActivity {
    public Static FatherClass STATIC_SON;

    @override
    protected void onCreate(Bundle savedInstance){
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme);
        setContentView(R.layout.activity_main);
        
        // I got this condition from external 
        if(loadExternalCondition()){
            STATIC_SON = new FirstSonClass(4);
        }else {
            STATIC_SON = new SecondSonClass(43);
    }

}

Now in one Fragment I have an customView and I want to show this customView only if the object is instance of FirstSonClass .现在在一个Fragment中,我有一个customView ,只有当objectFirstSonClass customView I don't know which format should the type got.我不知道该type应该采用哪种格式。 I am thinking to make it string and pass it here.我想把它串起来并在这里传递。 and in CustomView class maybe in some way I can generate a class name.在 CustomView class 中,也许我可以以某种方式生成 class 名称。

<LinearLayout

    <com.whatEver.CustomView
        android:id="@+id/first_view"
        app:type="FirstSonClass"

    </com.whatEver.CustomView>


    <com.whatEver.CustomView
        android:id="@+id/second_view"
        app:type="SecondSonClass"

    </com.whatEver.CustomView>
</LinearLayout>

Now I just want that customView decide to show itself depending on the static class, and the passed class, if the both instanced from the same class then it should shows itself, otherwise hide itself. Now I just want that customView decide to show itself depending on the static class, and the passed class, if the both instanced from the same class then it should shows itself, otherwise hide itself.

public CustomView(context, AttributeSet attrs){
     super(context,attrs);
     init(context,attrs);
}

private init(context, AttributeSet attrs){
     TypedArray typedArray = context.obtainStyledAttributes(atrrs, R.styleable.CustomView);
    try {
         FatherClass father =typedArray.getClass(R.styleable,CustomView_type);
        }
    } finally {
        typedArray.recycle();
    }    

    if(MainActivity.STATIC_SON instanceOf father){
       this.show()
    }else {
       this.hide()
    }
}

my aim is:我的目标是:

When STATIC_SON is instanceOf FirstSonClass then show firstView .STATIC_SON是 instanceOf FirstSonClass然后显示firstView And When is instanceOf SecondSonClass show secondView otherwise don't show anything.并且 instanceOf SecondSonClass显示secondView否则不显示任何内容。

No need to create instance to check types, object instanceOf class无需创建实例来检查类型, object instanceOf class

private init(context, AttributeSet attrs){
    if(MainActivity.STATIC_SON instanceOf FirstSonClass){
       this.show()
    }else {
       this.hide()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单的 XML 框架和 Android - 奇怪的 class 属性 - Simple XML Framework and Android - strange class attribute 如何动态地将类名传递给Testng XML中的类标记 - How to pass class name dynamically in to class tag in Testng XML 如何将类名作为参数传递给android中的函数? - How to pass class name as parameter to a function in android? 将点击按钮的名称传递给Android中的其他类 - Pass the name of button clicked to different class in Android Android活动:输入数字,按下按钮,从xml传递到类 - Android Activity: Input number, press button, pass from the xml to the class XML和JAXB:将属性传递给值 - XML & JAXB: pass attribute into value 如何将作为“值”调用注释的类名和方法传递给注释属性 - How can I pass the class name and method on which the annotaion being invoked as a "value" to the annotation attribute Java / JAXB:将具有相同名称但不同属性值的XML元素解组为不同的类成员 - Java/JAXB: Unmarshall XML elements with same name but different attribute values to different class members 改变<application android:name="”${applicationName}”"> Flutter 项目的属性 (AndroidManifest.xml)</application> - Changing the <application android:name=”${applicationName}”> attribute of a Flutter proyect (AndroidManifest.xml) 在Android中搜索具有另一个属性的XML属性 - Search for XML attribute with another attribute in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM