Sum*_*hoo 7 android togglebutton android-actionbar
我是否可以知道如何在ActionBar中添加Switch小部件并处理click事件或切换更改事件.
现在我可以在ActionBar中扩展Switch,但无法响应change事件.我在下面添加了main.xml.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.MainActivity" >
<item
android:id="@+id/toggleservice"
android:actionViewClass="android.widget.Switch"
android:showAsAction="ifRoom"
android:title="@string/toggle_service"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
我想在用户点击开关并更改其状态时启动服务.任何帮助都非常感谢.
adn*_*eal 16
你需要打电话MenuItem.getActionView
,这是一个例子:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate your Menu
getMenuInflater().inflate(R.menu.your_menu, menu);
// Get the action view used in your toggleservice item
final MenuItem toggleservice = menu.findItem(R.id.toggleservice);
final Switch actionView = (Switch) toggleservice.getActionView();
actionView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Start or stop your Service
}
});
return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6988 次 |
最近记录: |