如何为Android本机设置应用程序进行SwitchReference的设置导航?
当你点击WiFi区域(不在交换机上)时,它将导航到一个新屏幕:

我的应用程序只将开关从ON更改为OFF,反之亦然,即使我没有点击开关.
我正在使用PreferenceFragment和xml的屏幕.我的偏好是遵循Android PreferenceFragment文档中的示例.我在ICS 4.0 API 14上开发我的应用程序.
有人知道怎么做吗?
编辑:
我的XML看起来像这样:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:layout="@layout/preference_category"
android:title="User Settings" >
<SwitchPreference
android:key="pref_autorun"
android:layout="@layout/preference"
android:summary="Autorun SMODE on boot"
android:title="Autorun SMODE" />
<SwitchPreference
android:key="pref_wifi_control"
android:layout="@layout/preference"
android:selectable="false"
android:summary="Controls your Wi-Fi radio automatically based on hotspot availability"
android:title="Wi-Fi Radio Control" />
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud) 我试图添加一个开关(复选框作为第二个选项)到导航抽屉."幻灯片菜单".使用导航抽屉创建新项目时将获得的默认值.
我尝试过一个全新的项目,所以我不会弄乱我的"真实"项目.
但没有任何运气.似乎找不到其他值得一提的东西..
我试图在最后一个menuItem添加开关.activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_camera"
android:icon="@drawable/ic_menu_camera"
android:title="Import"
android:checkable="true"/>
<item
android:id="@+id/nav_gallery"
android:icon="@drawable/ic_menu_gallery"
android:title="Gallery" />
<item
android:id="@+id/nav_slideshow"
android:icon="@drawable/ic_menu_slideshow"
android:title="Slideshow" />
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Tools" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Share" />
<item
android:id="@+id/nav_send"
android:icon="@drawable/ic_menu_send"
android:title="Send" />
<item
android:id="@+id/myswitch"
android:title=""
android:actionLayout="@layout/ttt"
/>
</menu>
</item>
</menu>
Run Code Online (Sandbox Code Playgroud)
ttt.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/ss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
最后一项"id/myswitch"根本没有显示.MainActivity.java是100%默认值.这就是为什么我不发布它.
我发现这篇文章如何添加一个开关到android动作栏? 这对我有用.但我不能得到它的事件.我正在使用appcompat,我使用了app命名空间for actionLayout和showAsAction,但是我无法处理它对onOptionsItemSelected方法的点击?我的应用程序在启动时崩溃了.如何处理它或这段代码有什么问题?这是我的代码
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"/>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
@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);
}
Run Code Online (Sandbox Code Playgroud)
和
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = …Run Code Online (Sandbox Code Playgroud) 我已经使用Android的Preference API设计了一个设计设计.
但我不知道如何创建"主开/关开关".这里提到http://developer.android.com/design/patterns/settings.html但没有关于如何实现它的文档.
从技术上讲,它是位于操作栏中的SwitchPreference,它在关闭时禁用所有子设置.
有任何想法吗?