如何实现具有复选框的三个阶段列表项?

时间:2016-07-06 14:45:12

标签: android checkbox

我是Android开发的新手,我希望能够在wifi或蓝牙活动中实现类似的功能..

  1. 单击“打开蓝牙”列表项显示为临时禁用效果(灰色)

  2. 同时以下文字从“点击打开”变为“开启”

  3. 打开后,最后启用列表项(选中复选框)

  4. 请帮帮我......

1 个答案:

答案 0 :(得分:0)

我假设您知道如何创建布局,将其绑定到活动并使用findViewById获取视图。如果你不只是谷歌

实现您想要的效果非常简单。这就是你如何做到的:

  • 在布局中使用开关(see here)。它是显示开关按钮的标准视图

  • 设置开关文字和onCheckedChangeListener

  • 工作完成后更新开关状态(按照示例,激活蓝牙或wifi)

这是一个极小的工作示例:

public class MainActivity extends AppCompatActivity {

   Switch testSwitch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //get the switch from layout
    testSwitch = (Switch) findViewById(R.id.test_switch);

    //set text
    testSwitch.setText("Example switch");

    //set on checked change listener
    testSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@
        Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            //set switch text based on checked status
            if (isChecked) {
                compoundButton.setText("Turning on");
            } else {
                compoundButton.setText("Turning off");
            }

            //disable it
            compoundButton.setEnabled(false);

            //start the big long process (activate bluetooth or wifi). That's only an example
            afterSomeTime();
        }
    });
}
public void afterSomeTime() {
    //After 5 seconds update the switch
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            testSwitch.setEnabled(true);
            if (testSwitch.isChecked()) {
                testSwitch.setText("ON");
            } else {
                testSwitch.setText("OFF");
            }
        }
    }, 5000);
}
}

希望这有点帮助

相关问题