切换按钮没有出现在动作条? [英] Switch button not showing up in ActionBar?

查看:269
本文介绍了切换按钮没有出现在动作条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个开关,我把到我的动作条,但它似乎并没有显示出来,我不明白为什么。这是我尝试:

I have a Switch that I placed into my ActionBar, but it doesn't seem to show up and I don't see why. This was my attempt:

create_post_menu.xml

create_post_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".CreatePost">

    <item
        android:id="@+id/toggle_test"
        android:title=""
        app:showAsAction="ifRoom"
        android:orderInCategory="1"
        android:actionViewClass="android.widget.Switch" />

    <item
        android:id="@+id/send_post"
        android:orderInCategory="2"
        android:title="Send"
        app:showAsAction="ifRoom" />

</menu>

CreatePost.java

CreatePost.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.create_post_menu, menu);
    // Get the action view used in your toggleservice item
    final MenuItem toggleservice = menu.findItem(R.id.toggle_test);
    final Switch actionView = (Switch) toggleservice.getActionView();
    actionView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Start or stop your Service
        }
    });
    return super.onCreateOptionsMenu(menu);
}

我试图实例化开关,看看我是否可以设置一个侦听器来查看,如果你点击或关闭开关,但我似乎无法实例化它,因为我得到一个错误试图创建<$ C $在CreatePost.java C>的actionView 变量。谁能帮助我?谢谢!

I tried to instantiate the Switch to see if I can set a listener to see if you click on or off for the switch but I can't seem to instantiate it as I get an error trying to create the actionView variable in CreatePost.java. Can anyone help me with this? Thanks!

推荐答案

您收到错误,因为你的actionView为空。改变你切换菜单code

your are getting error because your actionView is null. change your Switch menu code

<item
     android:id="@+id/toggle_test"
     android:title=""
     app:showAsAction="ifRoom"
     android:orderInCategory="1"
     android:actionViewClass="android.widget.Switch" /> 

应用:actionViewClass =android.widget.Switch小心地将应用不是<$看C $ C>机器人这样的...

to app:actionViewClass="android.widget.Switch" look carefully it would be app not android like this...

<item
      android:id="@+id/toggle_test"
      android:title=""
      app:showAsAction="ifRoom"
      android:orderInCategory="1"
      app:actionViewClass="android.widget.Switch" /> 

和现在改变你的java code这样的

and now change your java code like this

final MenuItem toggleservice = menu.findItem(R.id.toggle_test);
Switch actionView=(Switch) MenuItemCompat.getActionView(toggleservice );
actionView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // Start or stop your Service
    }
});

这篇关于切换按钮没有出现在动作条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆