获取按钮以便在检查时执行某些操作

时间:2016-04-21 14:48:27

标签: android

我有多个按钮,由这段代码管理:

ToggleButton.OnCheckedChangeListener listener = new ToggleButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton v, boolean isChecked) {
        int index;
        for(index = 0; index < viewIds.length; index++) {
            if (v.getId() == viewIds[index]) {
                getSharedPreferences("PREFS_NAME", MODE_PRIVATE)
                        .edit()
                        .putBoolean(stringIds[index], isChecked)
                        .apply();

                break;
            }
        }
    }
};

我创造了方法    scanButton(); 像这样:

  private void scanButton() {

    Button scan = (Button)findViewById(R.id.button_scan);
    scan.setOnClickListener(new View.OnClickListener() {

        @Override
                public void onClick(View v) {
            //here what will be done when button is clicked
            Log.d(TAG, "Button \"SCAN\" is pressed");
            Toast.makeText(RandDActivity.this, "Scan pressed", Toast.LENGTH_LONG)
                    .show();

            //WHAT HERE

        }
    });


}

现在问题:在这里放置什么只能获得那些isChecked with&#34; true&#34;说要做什么?

1 个答案:

答案 0 :(得分:0)

首先,您需要查看用户是否已选中该按钮,您可以加载已经保存在onCheckedChanged()方法中的SharedPreferences,将其保存为布尔变量,然后设置条件如下:< / p>

CollectionController *collectionController = [self.storyboard instantiateViewControllerWithIdentifier:@"Calculate"];
        [self addChildViewController:collectionController];
        collectionController.view.frame = self.calculationView.bounds;
        [self.pageControllerView addSubview:collectionController.view];
        [collectionController didMoveToParentViewController:self];

我假设isChecked是您的布尔变量,您只需使用从SharedPreferences获得的值设置。