2016-07-27 55 views
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(); 

回答

0

你的问题有点不清楚。
我想你想从具有ActionBar的Activity改变BlinkFragment的视图。

以下代码将帮助您?

呼叫BlinkFragment的从YourActivity方法:

public class YourActivity extends AppCompatActivity { 

    ... 

    private BlinkFragment getBlinkFragment() { 
     return (BlinkFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2); 
    } 

    @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(); 
       getBlinkFragment.onDisconnected(); 

       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 
       } 
       getBlinkFragment.onLedControlChanged(ledControl); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
    .... 
} 

BlinkFragment会象下面这样:

public class BlinkFragment extends Fragment implements View.OnClickListener { 

    ... 

    public void onLedControlChanged(LedControl ledControl) { 
     // change the view 
     discrete_bl_g.setEnabled(ledControl.instance.data.expertMode); 
     discrete_bl_h.setEnabled(ledControl.instance.data.expertMode); 
    } 

    public void onDisconnected() { 
     // change the view 
    } 
} 
+0

我想要做的是: –

+0

我有一个片段的布局和一些按钮从操作栏中的复选框我禁用并启用按钮,但问题是在布局没有影响,直到我重新加载布局。 –

+0

好吧,我想了解你的问题。我的代码在你的项目中不起作用? – nshmura