本文介绍了RadioGroup.setEnabled(false)不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用setEnabled(false)将其设置为无法使用,但是它不起作用,并且在此方法之后,RadioGroup.isEnabled()的值为false.值已更改.

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.

该代码来自《 Android编程指南》.

The code is from Android Programming Guide.

PS:Spinner组件也使用setEnabled(false).

代码如下:

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);
    }
}

}

推荐答案

使用以下方法:

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

这篇关于RadioGroup.setEnabled(false)不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:48