如何在Android Actionbar开关中获取/设置Action事件 [英] How to get/set action event in Android actionbar switch
问题描述
我发现了此帖子如何向Android添加开关动作栏?,这对我有用.但是我无法得到它的事件.我正在使用appcompat,并且为actionLayout和showAsAction使用了应用程序名称空间,但无法处理onOptionsItemSelected方法上的单击?我的应用在启动时崩溃.如何处理它或此代码有什么问题?这是我的代码
I found this post How to add a switch to android action bar? and this works for me. But I can't get its event. I'm using appcompat, and I used app namespace for actionLayout and showAsAction, but I'm not able to handle its click on onOptionsItemSelected method?. My app is crashing on start up. how to handle it or what is wrong with this code? here is my code
menu_main.xml
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/switch_layout"/>
switch_layout.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/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
</RelativeLayout>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
MenuItem menuItem= menu.findItem(R.id.myswitch);
Switch switcha = (Switch)menuItem.findViewById(R.id.switchForActionBar);
switcha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do anything here on check changed
}
});
return super.onCreateOptionsMenu(menu);
}
和
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.myswitch) {
//do something
}
return super.onOptionsItemSelected(item);
}
我们非常感谢您的帮助.预先感谢.
Any help is highly appreciated. Thanks in advance.
推荐答案
我发现了我的错误.我必须使用
I found my error. I have to use
View view = MenuItemCompat.getActionView(menuItem);
View view = MenuItemCompat.getActionView(menuItem);
onCreateOptionsMenu中的额外行以查找切换按钮.这是我的完整方法
extra line in onCreateOptionsMenu to find out the switch button . Here is my full method
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.myswitch);
View view = MenuItemCompat.getActionView(menuItem);
Switch switcha = (Switch) view.findViewById(R.id.switchForActionBar);
switcha.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do anything here on check changed
}
});
return super.onCreateOptionsMenu(menu);
}
这篇关于如何在Android Actionbar开关中获取/设置Action事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!