首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将切换按钮添加到菜单以停止并启动服务

将切换按钮添加到菜单以停止并启动服务
EN

Stack Overflow用户
提问于 2015-07-14 07:53:36
回答 6查看 19.7K关注 0票数 10

我试图使用户能够停止并启动服务,我正在从菜单中实现该服务,当用户单击该菜单时文本将被更改,因此我希望在菜单工具中添加ToggleButton作为选项,但在我的情况下现在没有显示任何内容。我怎么才能修好它?

AndroidManifest.xml

代码语言:javascript
运行
复制
<?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>

MainActivity:

代码语言:javascript
运行
复制
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;
        }
    }
}

编辑:

代码语言:javascript
运行
复制
@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;
    }
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2015-07-14 08:06:34

如前所述,不能将切换按钮添加到菜单中。您可以使用菜单项中的android:checkable属性来处理这两种状态。

类似于:

菜单

代码语言:javascript
运行
复制
<item
    android:id="@+id/checkable_menu"
    android:checkable="true"
    android:title="@string/checkable" />

活性

代码语言:javascript
运行
复制
private boolean isChecked = false;

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem checkable = menu.findItem(R.id.checkable_menu);
    checkable.setChecked(isChecked);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.checkable_menu:
            isChecked = !item.isChecked();
            item.setChecked(isChecked);
            return true;
        default:
            return false;
    }
}

PS:从这里复制代码。

您只需在单击事件上更新项目图标,就可以用item.setIcon(yourDrawable));显示这两种状态

票数 12
EN

Stack Overflow用户

发布于 2016-01-13 07:10:47

这很简单。相反,您将有您的按钮在工具栏上。

代码语言:javascript
运行
复制
<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布局。

代码语言:javascript
运行
复制
<?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>

在代码中:

代码语言:javascript
运行
复制
 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
            }
        });

输出!

它很大,但是很明显,你可以调整它的大小。

票数 15
EN

Stack Overflow用户

发布于 2018-05-01 17:20:25

我知道发帖的时间很长,但它可能会对某人有所帮助:)

我遵循这个链接并更新了一些已实现的解决方案,因为应用程序在这些修改之前崩溃了。

下面是完整的解决方案: 1-在layout文件夹下创建一个新的xml文件,并将其命名为switch_layout.xml,并将以下内容放在下面:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <Switch
        android:id="@+id/switchAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />
</RelativeLayout>

2-在菜单文件夹下的main.xml文件中添加以下菜单项:

代码语言:javascript
运行
复制
<item
    android:id="@+id/switchId"
    android:title=""
    app:actionLayout="@layout/switch_layout"
    app:showAsAction="always" />

3-转到您的活动中,下面是onCreateOptionsMenu方法的完整实现:

代码语言:javascript
运行
复制
 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = (MenuItem) menu.findItem(R.id.switchId);
    item.setActionView(R.layout.switch_layout);
    Switch switchAB = item
            .getActionView().findViewById(R.id.switchAB);
    switchAB.setChecked(false);

    switchAB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            if (isChecked) {
                Toast.makeText(getApplication(), "ON", Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(getApplication(), "OFF", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    });
    return true;
}
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31400870

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档