android - 在菜单中添加切换按钮以停止和启动服务

标签 android android-layout android-menu

我试图让用户停止和启动我从菜单实现的服务,当他点击它时,文本将被更改,所以我想添加 ToggleButton 作为选项菜单工具,但现在我的案例中没有显示任何内容。我该如何解决?

AndroidManifest.xml

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

    <ToggleButton
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOff="Off"
        android:textOn="On" />
</menu>

主要 Activity :

public class MainActivity extends ActionBarActivity {
    ToggleButton tButton;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.toggle:

                tButton = (ToggleButton) findViewById(R.id.toggle);
                tButton.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (((ToggleButton) v).isChecked()) {
                            Intent i = new Intent(MainActivity.this,
                                                  TrackingService.class);
                            startService(i);
                            System.out.println("test is checked, start service");

                        } else {
                            // Stop the service when the Menu button clicks.
                            Intent i = new Intent(MainActivity.this,
                                                  TrackingService.class);
                            stopService(i);
                            System.out.println("test is NOT checked, stop service");

                        }

                    }
                });

                return true;

            default:
                return false;
        }
    }
}

编辑:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.checkable_menu:
        if (isChecked = !item.isChecked()) {
            item.setChecked(isChecked);

            Intent i = new Intent(this, TrackingService.class);
            startService(i);
            System.out.println("test if onOptionsItemSelected");
        } else {
            Intent i = new Intent(this, TrackingService.class);
            stopService(i);
            System.out.println("test else onOptionsItemSelected");

        }
        return true;

    default:
        System.out
                .println("test default onOptionsItemSelected was invoked.");
        return false;
    }
}

最佳答案

这很容易。相反,您将在工具栏上看到切换按钮。

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

这是您的show_protected_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
                }Y
            }
        });

输出!

enter image description here

它很大,但显然你可以调整它的大小

关于android - 在菜单中添加切换按钮以停止和启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31400870/

相关文章:

android - Fragment 和 FragmentActivity 有什么区别?

android - 使用 ActionBar 后退按钮时的共享元素转换

android - 在 Android 中将布局添加为 View

android - Android 上的 FCM 通知可以覆盖之前的通知吗?

java - 第二个按钮上的 OnClickListener 既没有正确初始化也没有按预期工作

android - 自定义 AppCompat 主题未更改旧设备上的溢出图标

android - 在 Android 中以编程方式分配 View ID

java - 动态 Xml(我认为)

java - 在android中编辑选项菜单项内容/设置

android - 如何以编程方式触发/单击 Android 中的 MenuItem?