简体   繁体   English

设置在Spinner上选择的基于TableLayout的可见项

[英]Set visibility TableLayout based item selected on Spinner

I want to make Spinner to set Visibility of table, i have 2 Array String "cuboid and cylinder". 我想让Spinner设置表的可见性,我有2个数组字符串“cuboid and cylinder”。 if i select Cuboid , cubeT table is visible and cyclinderT table is Invisible. 如果我选择Cuboid,cubeT表是可见的,而cyclinderT表是不可见的。 and if i select Cylinder , cylinderT table is Visible and cubeT is Invisible. 如果我选择Cylinder,cylinderT表是可见的,cubeT是不可见的。

Sample code welcome. 示例代码欢迎。 Thank you for your time. 感谢您的时间。

You can set an OnItemSelectedListener to your Spinner then using the int position argument to decide what action to take. 您可以将OnItemSelectedListener设置为Spinner,然后使用int position参数来决定要采取的操作。

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                switch (position) {
                    case Cuboid: 
                       cubeT.setVisibility(View.VISIBLE);
                       cylinderT.setVisibility(View.GONE);
                    break;

                    ....
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) { }

        });
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        String text = ((Spinner)spinner).getSelectedItem().toString();
        if (Intrinsics.areEqual(text, "Cuboid")) {
            //Your code here to set your "table" as cubeT if it's image in imageview
            //if it's a "tableLayout" you may create 2 different layouts included and..:
            setContentView(R.layout.your_cubeT_layout);         
        } else if (Intrinsics.areEqual(text, "Cylinder")) {
            setContentView(R.layout.your_cyclinderT_layout);
          }
    } //when it comes to use different layouts on the same activity, generally suggestions made over fragments to make your code more dynamic but i don't know how to do that...

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});

I'm not using java since years so i might code bad... So no warranty! 我好几年没用java了,所以我可能代码不好......所以不保修!

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

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