Preferencescreen中利用intent跳转activity

设置中的Preferencescreen不仅可以作为设置界面显示,而且还能够启动activity,下面主要是对启动activity的介绍
1. Preferencescreen中启动activity

例如wireless_setting.xml中有如下片段

Html代码 
  1. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:settings="http://schemas.android.com/apk/res/com.seedshope.android">  
  3.     <PreferenceScreen  
  4.         android:key="wifi_settings"  
  5.         android:title="@string/wifi_settings"  
  6.         android:summary="@string/wifi_settings_summary" >  
  7.         <intent  
  8.             android:action="android.intent.action.MAIN"  
  9.             android:targetPackage="com.android.settings"  
  10.             android:targetClass="com.android.settings.wifi.WifiSettings" />  
  11.     </PreferenceScreen>  
  12. </PreferenceScreen>  
其中com.android.settings是工程的包名,com.android.settings.wifi.WifiSettings是要启动的类。另外,xmlns:settings这一项没有也是可以的(那它的作用是什么?)。
一般情况下,这样就ok了,点击对应的Preference就能够启动对应的activity,然而仅仅这样有时这样,并不一定能够启动对应的activity,因为它与回调函数onPreferenceTreeClick有关,有时候我们会重写这个方法如:

Java代码 
  1. public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {  
  2.        if ( (preference == mAirplaneModePreference) &&  
  3.                 (Boolean.parseBoolean(  
  4.                     SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) ) {  
  5.             // In ECM mode launch ECM app dialog  
  6.            startActivityForResult(  
  7.                 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),  
  8.                 REQUEST_CODE_EXIT_ECM);  
  9.             return true;  
  10.         }  
  11.         else {  
  12.             // Let the intents be launched by the Preference manager  
  13.             return false;  
  14.        }  
  15.     }  
其中WirelessSetting.java中有如上函数,当返回值为true时,你怎么点击Preference都不会跳转到activity的,只有等到它的返回值为false的时候,才可以正常跳转,因为点击某一个Preference的真正实现在Preference.java的performClick函数中如下:

Java代码 
  1. void performClick(PreferenceScreen preferenceScreen) {  
  2.           
  3.         if (!isEnabled()) {  
  4.             return;  
  5.         }  
  6.           
  7.         onClick();  
  8.           
  9.         if (mOnClickListener != null && mOnClickListener.onPreferenceClick(this)) {  
  10.             return;  
  11.         }  
  12.           
  13.         PreferenceManager preferenceManager = getPreferenceManager();  
  14.         if (preferenceManager != null) {  
  15.             PreferenceManager.OnPreferenceTreeClickListener listener = preferenceManager  
  16.                     .getOnPreferenceTreeClickListener();  
  17.             if (preferenceScreen != null && listener != null  
  18.                     && listener.onPreferenceTreeClick(preferenceScreen, this)) {  
  19.                 return;  
  20.             }  
  21.         }  
  22.           
  23.         if (mIntent != null) {  
  24.             Context context = getContext();  
  25.             context.startActivity(mIntent);  
  26.         }  
  27.     }  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用自定义布局文件来定义 PreferenceScreen 的样式。以下是实现步骤: 1. 创建一个 XML 布局文件作为 PreferenceScreen 的子项的布局。例如,创建一个名为 `preference_item.xml` 的布局文件。 2. 在 PreferenceScreen 使用 `android:layout` 属性来指定自定义布局文件。 例如,以下代码将使用 `preference_item.xml` 文件作为 PreferenceScreen 的子项布局: ```xml <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <Preference android:key="example_preference" android:title="Example Preference" android:layout="@layout/preference_item" /> </PreferenceScreen> ``` 3. 在自定义布局文件,可以使用任何布局和视图来构建自己的布局。例如,以下代码创建了一个带有图标、标题和摘要的 LinearLayout: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:padding="16dp"> <ImageView android:id="@android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="16dp"> <TextView android:id="@android:id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Example Preference Title" android:textStyle="bold" /> <TextView android:id="@android:id/summary" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@android:id/title" android:text="Example Preference Summary" /> </RelativeLayout> </LinearLayout> ``` 通过这种方式,您可以创建自己的 PreferenceScreen 布局,并根据需要定制其外观和行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值