Android的:如何获得使用的ToggleButtons一个RadioGroup中? [英] Android: How to get a radiogroup with togglebuttons?

查看:202
本文介绍了Android的:如何获得使用的ToggleButtons一个RadioGroup中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一组按钮,其中用户可以选择其中一个作为选项。它要像行为RadioButtonGroup中,但我不希望无线圈为present。我只是希望用户只能够切换按钮中的一个。

I want a group of buttons where a user can choose one of them as option. It has to be a radiobuttongroup like behaviour, but I don't want the radio circle to be present. I just want the user to be able to toggle only one of the buttons.

我想我会需要像成才一个togglegroup。

I think I would need someting like a togglegroup.

请问这样的事情存在于Android的?

Does something like this exist in Android?

推荐答案

我刚刚的再利用的的RadioGroup中,像这样:(请注意onclick属性,即单击按钮会触发你的活动的onToggle(查看)方法。

I'd just re-use the RadioGroup like so: (please note the onClick attribute,i.e. a button click will trigger your Activity's onToggle(View) method.

    <RadioGroup android:id="@+id/toggleGroup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:orientation="horizontal"
            >

        <ToggleButton android:id="@+id/btn_Letter"
                      android:layout_height="wrap_content"
                      android:layout_width="fill_parent"
                      android:layout_weight="1"
                      android:textSize="14sp"
                      android:textOn="Letter"
                      android:textOff="Letter"
                      android:onClick="onToggle"
                      android:checked="true"
                />
        <ToggleButton android:id="@+id/btn_A4"
                      android:layout_height="wrap_content"
                      android:layout_width="fill_parent"
                      android:layout_weight="1"
                      android:textSize="14sp"
                      android:textOn="A4"
                      android:textOff="A4"
                      android:onClick="onToggle"
                />
</RadioGroup>

在你的活动,或别的地方,你可以定义一个监听器,如:

In your Activity, or some place else, you can define a listener, e.g.

static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(final RadioGroup radioGroup, final int i) {
            for (int j = 0; j < radioGroup.getChildCount(); j++) {
                final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j);
                view.setChecked(view.getId() == i);
            }
        }
    };

和中的onCreate()进行注册,例如:

and register it, for instance in onCreate():

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.scan_settings);

        ((RadioGroup) findViewById(R.id.toggleGroup)).setOnCheckedChangeListener(ToggleListener);    
    }

终于在onToggle(视图),你会做什么需要发生,具体到你的应用程序。同时也呼吁RadioGroup中的检查方式,与切换视图的ID。像这样:

finally in onToggle(View), you would do whatever need to happen, specific to your app. and also call the RadioGroup's check method, with the toggled view's id. Like so:

public void onToggle(View view) {
    ((RadioGroup)view.getParent()).check(view.getId());
    // app specific stuff ..
}

这篇关于Android的:如何获得使用的ToggleButtons一个RadioGroup中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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