RadioGroup,setEnabled(false)not working!

人盡茶涼 提交于 2019-12-20 16:16:07

问题


I have used setEnabled(false) to set it unable,But it doesn't work. and after this method ,the value of RadioGroup.IsEnabled() is false. The value was changed.

The code is from Android Programming Guide. Ps: The Spinner component use the setEnabled(false) is well.

code is as follows:

package com.example.testviews;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;

public class TestRadioGroup extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.radiogroup);

    final RadioGroup testRadioGroup = (RadioGroup) findViewById(R.id.testRadioGroup);

    final Button changeEnabledButton = (Button) findViewById(R.id.changeEnabledButton);
    changeEnabledButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            changeEnabled(testRadioGroup);
        }
    });

    final Button changeBgColorButton = (Button) findViewById(R.id.changeBackgroundColorButton);
    changeBgColorButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            changeBgColor(testRadioGroup);
        }
    });

}

protected void changeBgColor(RadioGroup testRadioGroup) {
    // TODO Auto-generated method stub
    testRadioGroup.setBackgroundColor(Color.BLUE);
}

protected void changeEnabled(RadioGroup testRadioGroup) {
    // TODO Auto-generated method stub
    if (testRadioGroup.isEnabled()) {
        testRadioGroup.setEnabled(false);
    } else {
        testRadioGroup.setEnabled(true);
    }
}

}


回答1:


use the following method:

for (int i = 0; i < testRadioGroup.getChildCount(); i++) {
    testRadioGroup.getChildAt(i).setEnabled(false);
}



回答2:


Views can be compose by multiple touchable elements. You have to disable them all, like this:

for(View lol : your_spinner.getTouchables() ) {
    lol.setEnabled(false);
}

If it is a simple one since it also returns itself:

Find and return all touchable views that are descendants of this view, possibly including this view if it is touchable itself.

View#getTouchables()




回答3:


You can't use the following code;

for(View lol : your_spinner.getTouchables() ) {
    lol.setEnabled(false);
}

Once the views has disabled, there is no touchable child/descentant views anymore.



来源:https://stackoverflow.com/questions/13261602/radiogroup-setenabledfalsenot-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!