
在xml文件的Preference标签中,我们可以添加intent来为我们快速实现一种意图,比如说快速打开一个网页,或者快速启动一个Activity等等,还可以使用extra给intent标签加参数来传递参数(再通过getIntent().getStringExtra(“key”)来获取)。
点击这个Preference则会自动去调用浏览器打开网页
<Preference
android:title="Click me open the web"
android:key="key_pref_intent"
>
<intent android:action="android.intent.action.VIEW"
android:data="http://.hao123.com"/>
<!--可以通过<extra>传附加信息getIntent().getStringExtra("reused_key") -->
<extra
android:name="key"
android:value="value"/>
</Preference>
启动指定类
<Preference
android:title="PREFERENCE TITLE"
>
<!-- android:targetPackage是应用程序的包名,而android:targetClass的路径在子包下的类 -->
<!-- android:targetPackage设置为子包,运行时则找不到Activity -->
<intent
android:action="ACTION_A_INTENT"
android:targetPackage="com.crazy.training"
android:targetClass="com.crazy.training.ui.MainActivity">
</intent>
</Preference>
PreferenceScreen和PreferenceCategory没有新增的属性,所有属性全部继承自Preference。其中PreferenceScreen作为顶级容器,PreferenceCategory作为次级容器(类似于SQL Group by功能暂且这么理解吧),虽然他们也是可以单独使用的,但是并不能响应onPreferenceClick和onPreferenceChange事件。
布局和MainActivity的代码依然很简单和前面类似
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="PreferenceScreen">
<PreferenceCategory
android:key="key_prerence"
android:summary="Preference Categories Summary"
android:title="Preference Categories">
</PreferenceCategory>
</PreferenceScreen>
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-49635-5.html
战争其实离我们不远
屌丝也不会经常喝