在菜单中添加切换按钮以停止和启动服务
Add toggle button to the menu to stop and start service
我正在尝试让用户停止和启动我正在从菜单中实现的服务,当他单击它时,文本将被更改,所以我想在菜单中添加 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>
主要活动:
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;
}
}
您不能将任何小部件放入 <menu>
并期望它能正常工作。你可以放在那里的是 documented here,它基本上仅限于菜单 <item>
和 <group>
。不支持按钮、切换和其他小部件。如果这就足够了,您可以在 <item>
上使用 android:checkable
或使用旧方法并根据状态更改菜单项(如果服务开启,则您的项目应显示为 turn service off
反之亦然)。
Menu resources 与传统布局不同;您不能简单地将小部件添加到其中并期望它们起作用。菜单资源中唯一允许的元素是 <item>
或 <group>
.
恐怕无法在菜单内使用自定义布局。您可能想要用 PopupWindow 替换整个菜单,并在那里提供您的布局。
您可能需要考虑两种选择:
- 使用常规菜单条目作为切换,或
- 将 ToggleButton 直接放在 Actionbar/Toolbar 内,而不是放在菜单内。
在菜单 xml 中添加项目而不是小部件(没有 buttons/textviews 等)
您只需为该项目指定一个 ID 和一个 ICON,然后在您的活动 onCreateOptionsMenu() 方法中将它们膨胀。
然后有一个名为 onOptionsItemSelected(MenuItem item) 的方法根据您期望的 ID 检查项目 ID。
如果它等于你的切换服务选项确定服务状态和改变,如果你想有一个切换按钮功能你可以在这里使用item.setIcon(drawable)。
如上所述,您不能将切换按钮添加到菜单。您可以在菜单项中使用 android:checkable
属性 来处理这两种状态。
类似于:
菜单:
<item
android:id="@+id/checkable_menu"
android:checkable="true"
android:title="@string/checkable" />
Activity:
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:从here复制代码。
或您可以在点击事件上更新您的项目图标以显示带有item.setIcon(yourDrawable));
的两种状态
很简单。相反,您将在工具栏上看到切换按钮。
<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
}
});
输出!
它很大,但显然你可以调整它的大小
我知道 post 需要很长时间才能回答,但它可能会对某人有所帮助 :)
我遵循了这个 link 并更新了一些已实施的解决方案,因为应用程序在这些修改之前崩溃了
下面是完整的解决方案:
1- 在布局文件夹下创建一个新的 xml 文件并将其命名为 switch_layout.xml 并放入以下内容:
<?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 文件中添加以下菜单项:
<item
android:id="@+id/switchId"
android:title=""
app:actionLayout="@layout/switch_layout"
app:showAsAction="always" />
3- 转到您的 activity,下面是 onCreateOptionsMenu 方法的完整实现:
@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;
}
我正在尝试让用户停止和启动我正在从菜单中实现的服务,当他单击它时,文本将被更改,所以我想在菜单中添加 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>
主要活动:
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;
}
}
您不能将任何小部件放入 <menu>
并期望它能正常工作。你可以放在那里的是 documented here,它基本上仅限于菜单 <item>
和 <group>
。不支持按钮、切换和其他小部件。如果这就足够了,您可以在 <item>
上使用 android:checkable
或使用旧方法并根据状态更改菜单项(如果服务开启,则您的项目应显示为 turn service off
反之亦然)。
Menu resources 与传统布局不同;您不能简单地将小部件添加到其中并期望它们起作用。菜单资源中唯一允许的元素是 <item>
或 <group>
.
恐怕无法在菜单内使用自定义布局。您可能想要用 PopupWindow 替换整个菜单,并在那里提供您的布局。
您可能需要考虑两种选择:
- 使用常规菜单条目作为切换,或
- 将 ToggleButton 直接放在 Actionbar/Toolbar 内,而不是放在菜单内。
在菜单 xml 中添加项目而不是小部件(没有 buttons/textviews 等)
您只需为该项目指定一个 ID 和一个 ICON,然后在您的活动 onCreateOptionsMenu() 方法中将它们膨胀。
然后有一个名为 onOptionsItemSelected(MenuItem item) 的方法根据您期望的 ID 检查项目 ID。
如果它等于你的切换服务选项确定服务状态和改变,如果你想有一个切换按钮功能你可以在这里使用item.setIcon(drawable)。
如上所述,您不能将切换按钮添加到菜单。您可以在菜单项中使用 android:checkable
属性 来处理这两种状态。
类似于:
菜单:
<item
android:id="@+id/checkable_menu"
android:checkable="true"
android:title="@string/checkable" />
Activity:
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:从here复制代码。
或您可以在点击事件上更新您的项目图标以显示带有item.setIcon(yourDrawable));
很简单。相反,您将在工具栏上看到切换按钮。
<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
}
});
输出!
它很大,但显然你可以调整它的大小
我知道 post 需要很长时间才能回答,但它可能会对某人有所帮助 :)
我遵循了这个 link 并更新了一些已实施的解决方案,因为应用程序在这些修改之前崩溃了
下面是完整的解决方案: 1- 在布局文件夹下创建一个新的 xml 文件并将其命名为 switch_layout.xml 并放入以下内容:
<?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 文件中添加以下菜单项:
<item
android:id="@+id/switchId"
android:title=""
app:actionLayout="@layout/switch_layout"
app:showAsAction="always" />
3- 转到您的 activity,下面是 onCreateOptionsMenu 方法的完整实现:
@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;
}