2017-04-19 1 views
0

スイッチにメニューを追加しようとしています。スイッチがオンまたはオフのときにトーストを(少なくともこの時点で)したいと思っています。現在、何も起こっていないと私はなぜあまり理由がわからない。メニューのスイッチボタン、少なくともトゥルーまたは偽のときのトースト方法

メニュー項目とスイッチのレイアウトのコードを追加しました。

ありがとうございました!ここで

@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); 
} 

そしてまた、私は延期2でのMenuItem、Viewを導入しましたどのように注意してくださいライン。基本的にスイッチはどこにでもないが、正確にはメニュービューで探す必要があります。

その後、すべてが完全に機能します。

0

あなたはメニュー項目にスイッチやトグルボタンを追加することはできません。しかし、あなたが望むならツールバーでも同じことができます。

これまでに追加したメニュー項目に注意する必要があります。

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

をonOptionsItemSelectedしかし、まだあなたが表示したい場合は、ツールバーの概念を使用し、あなたがこの方法で同じの状態を変更することができます

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

チェック可能=「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 
      } 
     } 
    }); 
関連する問題