如何使UI组件消失,当选择某个单选按钮 [英] How to make UI components disappear when a certain RadioButton is selected

查看:225
本文介绍了如何使UI组件消失,当选择某个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个布局的XML文件,其中我有两个单选秒。

I have created a layout xml file where I have two RadioButtons.

在默认情况下,单选 1时,我在屏幕上显示一个的DatePicker 组件但当用户选择单选 2 的DatePicker 应该从屏幕消失。

By default, RadioButton 1 is selected and I am displaying a DatePicker component on the screen but when the user selects RadioButton2 the DatePicker should disappear from the screen.

我该如何处理此类情况?我应该在布局/ Java的code修改?

How can I handle the scenario? Should I make the change in layout / Java code?

推荐答案

这其实是很简单的。

获取的参考你的 RadioGroup中和您的的DatePicker 。实施 OnCheckedChangeListener RadioGroup中并检查哪个单选是在那里检查。

Get a reference of your RadioGroup and your DatePicker. Implement an OnCheckedChangeListener for the RadioGroup and check which RadioButton was checked in there.

如果单选 A被检查设置的知名度在的DatePicker 可见,如果单选 B被检查可见性设置为消失无形根据您的要求。

If RadioButton A was checked set the visibility on your DatePicker to visible and if RadioButton B was checked set the visibility to gone or invisible depending on your requirement.

作为一个例子。

public class MyActivity extends Activity {

    private RadioGroup choice;
    private DatePicker datePicker;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.your_layout);

        choice = (RadioGroup) findViewById(R.id.choice);
        datePicker = (DatePicker) findViewById(R.id.date_picker);

        choice.setOnCheckedChangeListener(
            new RadioGroup.OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId) {
                    case R.id.radio_button_a:
                        datePicker.setVisibility(View.VISIBLE);
                        break;
                    case R.id.radio_button_b:
                        datePicker.setVisibility(View.GONE);                    
                        break;
                }
            }
        });

    }
}

在理论上它应该是类似的东西。

In theory it should be something like that.

这篇关于如何使UI组件消失,当选择某个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆