0
我想檢查操作欄菜單中的選項後,從佈局更新某個按鈕的狀態。如何從操作欄更新佈局的內容?
我有一個操作欄菜單下面的代碼:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int delay = 150; // delay 150 ms.
switch (item.getItemId()){
case R.id.action_disconnect:
data.saveData(this.getApplicationContext());
Disconnect();
return true;
case R.id.action_check:
if (item.isChecked())
{
// here i want to update button status
item.setChecked(false);
ledControl.instance.data.expertMode = false;
ledControl.instance.data.configExpertMode = 1;
ledControl.instance.data.sendConfigTurnLightMessage();
}
else {
item.setChecked(true);
ledControl.instance.data.expertMode = true;
// here i want to update button status
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
這裏是佈局類控制按鈕加載爲片段
public class BlinkFragment extends Fragment implements View.OnClickListener {
private Switch switch_b1, switch_b2;
private DiscreteSeekBar discrete_bl_g, discrete_bl_h;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_blink, container, false);
switch_b1 = (Switch) v.findViewById(R.id.switch_b1);
switch_b2 = (Switch) v.findViewById(R.id.switch_b2);
discrete_bl_g = (DiscreteSeekBar) v.findViewById(R.id.discrete_bl_g);
discrete_bl_h = (DiscreteSeekBar) v.findViewById(R.id.discrete_bl_h);
discrete_bl_g.setEnabled(ledControl.instance.data.expertMode);
discrete_bl_h.setEnabled(ledControl.instance.data.expertMode);
}
}
我有這種方法試試:
fragment = new BlinkFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment2, fragment);
ft.commit();
我想要做的是: –
我有一個片段的佈局和一些按鈕從操作欄中的複選框我禁用並啓用按鈕,但問題是在佈局沒有影響,直到我重新加載佈局。 –
好吧,我想了解你的問題。我的代碼在你的項目中不起作用? – nshmura