【问题标题】:Android Studio -- Active Button when click checkboxAndroid Studio -- 单击复选框时的活动按钮
【发布时间】:2015-02-27 14:49:39
【问题描述】:

我的应用程序中有一个使用条款部分,我想在用户选中复选框时激活一个按钮。

【问题讨论】:

  • 如果您的复选框已选中,则启用按钮,否则禁用 it.set 按钮在 Main 类的 oncreate() 方法上的侦听器。 Android Studio 与它无关。

标签: android button checkbox android-studio


【解决方案1】:
public class MyActivity extends Activity {      
    protected void onCreate(Bundle icicle) {          
    super.onCreate(icicle);           
    setContentView(R.layout.content_layout_id);           
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);          
    final Button b = (Button)findViewById(R.id.button_id)

    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

            if (buttonView.isChecked()) {              
                    b.setEnabled(true);
            } else {
                    b.setEnabled(false)   
            }

        }

    });
}  
}

【讨论】:

  • 我没有看到这样做的意义,它总是会产生相同的结果,并且不会在复选框状态更改时更改按钮状态。
  • 你能解释一下吗
  • 当然 ;) 这段代码只会在创建 Activity 时执行一次,因此在 onCreate 中调用 checkBox.isChecked() 将始终返回 false,这是默认值。为了根据复选框的选中状态更改按钮状态,您需要附加一个 OnCheckedChangeListener 以便在用户按下复选框时激活回调。我希望这足够清楚,如果您需要进一步解释,请告诉我;)
猜你喜欢
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
相关资源
最近更新 更多