是这样的:
因为要在Action Bar 上弄个 Switch 按钮,所以我照
http://stackoverflow.com/questions/12107031/how-to-add-a-switch-to-and...
的方法做了个.
现在做到按钮了,但却不能使用onCheckedChanged来验测按钮的开启
我该怎样做?
这是我实现Action Bar 上 放Switch 的代码
mainmenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/menurelaty"
/>
</menu>
menurelaty.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Switch
android:id="@+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_centerVertical="true"
android:checked="false"
/>
</RelativeLayout>
我在想:会不会是因为MainActivity上的setContentView是main_activity.xml而不是实现Switch的menurelaty.xml才不能以findViewByid然后.onCheckedChanged 来验测按钮的开启
我也是参考这个来的,同样监听不到switch的onchangelistener
不过可以在onCreateOptionsMenu中监听即可
[java]
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.order, menu);
[/java]