SwitchCompat在menu中使用时出现的问题

来源:互联网 发布:mac全屏后切换桌面 编辑:程序博客网 时间:2024/06/03 15:59

在menu中使用
SwitchCompat需要监听时应该这样重写onCreateOptionsMenu(Menu menu)
即像这样写

@Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        final MenuItem item = menu.findItem(R.id.vibration_switch);        SwitchCompat mainSwitch = (SwitchCompat) item.getActionView();        mainSwitch.setOnCheckedChangeListener                (new CompoundButton.OnCheckedChangeListener() {                    @Override                    public void onCheckedChanged                            (CompoundButton buttonView, boolean isChecked) {                        if (isChecked) {                            Toast.makeText(MainActivity.this, "is", Toast.LENGTH_SHORT).show();                        } else {                            Toast.makeText(MainActivity.this, "not", Toast.LENGTH_SHORT).show();                        }                    }                });        return true;    }

研究了好久才发现menu中的不能直接在onCreate中监听
希望大家少走一些弯路

1 0