2017-04-19 15 views
0

因此,我試圖在菜單中添加一個開關,並且我希望(至少在此時)在開關打開或關閉時做一個Toast。目前沒有發生,我不太清楚爲什麼。菜單中的開關按鈕,如何至少在真或假時烤麪包

我添加了我的菜單項和開關佈局的代碼。

請指教,謝謝!

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.switch, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()){ 
     case R.id.myswitch: 
      Switch simpleSwitch = (Switch) findViewById(R.id.menuSwitch); 
      simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if (isChecked){ 
         Toast.makeText(ModularActivity.this, "This is on", Toast.LENGTH_SHORT).show(); 
        } else { 
         Toast.makeText(ModularActivity.this, "This is off", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
      return true; 
     case android.R.id.home: 
      // Navigate back to parent activity (CatalogActivity) 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 

    } 
    return true; 
} 

這是菜單項:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item 
    android:id="@+id/myswitch" 
    android:title="" 
    app:showAsAction="always" 
    app:actionLayout="@layout/activity_switch_layout" 
    /> 

這裏是activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="2dp" 
     android:textStyle="bold" 
     android:textColor="@android:color/white" 
     android:text="WHITE"/> 
    <Switch 
     android:id="@+id/menuSwitch" 
     android:checked="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="2dp" 
     android:text="GREEN" 
     android:layout_marginRight="5dp" 
     android:textStyle="bold" 
     android:textColor="@color/green"/> 

</LinearLayout> 

回答

0

試試你的代碼移動到onCreateOptionsMenu這樣的:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu, menu); 
    MenuItem menuItem = menu.findItem(R.id.myswitch); 
    View view = MenuItemCompat.getActionView(menuItem); 
    Switch simpleSwitch = (Switch) view.findViewById(R.id.menuSwitch); 
    simpleSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked){ 
       Toast.makeText(MainActivity.this, "This is on", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(MainActivity.this, "This is off", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

    return super.onCreateOptionsMenu(menu); 
} 

而且也請注意我是如何介紹弗里斯特兩個菜單項,並查看線。你基本上不必到處尋找你的開關,而是在你的菜單視圖中。

之後,一切正常。

0

菜單項不能添加交換機或切換按鈕。但是,如果你願意,你可以在工具欄上做同樣的事情。

在此之前,我需要注意您添加的菜單項。您可以添加機器人:可檢查=「真」

<item 
android:id="@+id/checkable_menu" 
android:checkable="true" 
android:title="@string/checkable" /> 

然後你就可以改變方法相同的onOptionsItemSelected狀態

case R.id.checkable_menu: 
     isChecked = !item.isChecked(); 
     item.setChecked(isChecked); 
     return true; 

但是,如果你想顯示仍然使用工具欄的概念開關或切換按鈕...

<item 
android:id="@+id/show_secure" 
android:enabled="true" 
android:title="" 
android:visible="true" 
app:actionLayout="@layout/show_protected_switch" 
app:showAsAction="ifRoom" /> 

這是你的switch.xml佈局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ToggleButton 
    android:id="@+id/switch_show_protected" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/switch_ptotected_btn_selector" 
    android:textOff="" 
    android:textOn=""/> 
</RelativeLayout> 

而在你的活動

ToggleButton mSwitchShowSecure; 
mSwitchShowSecure = (ToggleButton) menu.findItem(R.id.show_secure).getActionView().findViewById(R.id.switch_show_protected); 
mSwitchShowSecure.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if(b){ 
       //Your code when checked 

      } else { 
       //Your code when unchecked 
      } 
     } 
    }); 
相關問題