PreferenceActivity使用总结

来源:互联网 发布:太阳辐射强度数据查询 编辑:程序博客网 时间:2024/05/16 06:42

从名字应该可以看出 其实PreferenceActivity是 Activity 与 Perference 的混合物,Activity用于界面构建 Perference用于设置数据存放。

 常见属性设定  有哪几种方式:

1. CheckBox  用于 确定/取消  某项功能    如:是否静音   对应于:CheckBoxPreference   其在Preference 有一个选项与其自动绑定 下同

2.  ListView 用于列出所有选择  如:铃声选择 列出所有声音供选择使用  对应于:ListPreference

3.  属性结合 用于列出一系列相关属性 如:所有与短消息有关的功能 对应于:PreferenceScreen  该项仅用于界面 故与Preference无关

下面举一个我自己写的例子,用了PreferenceScreen属性:

布局文件:testing_setting.xml,用<PreferenceScreen>标签:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"

       android:title="@string/testing" >

    <!--TestingSub1 -->

    <PreferenceScreen

           android:title="@string/testingSub1">

        <intent android:action="android.intent.action.MAIN"

                android:targetPackage="com.ragentek.testingsettings"

                android:targetClass="com.ragentek.testingsettings.TestingSub"/>

    </PreferenceScreen>

    <!--TestingSub2 -->

    <PreferenceScreen

           android:title="@string/testingSub2">

        <intent android:action="android.intent.action.MAIN"

                android:targetPackage="com.ragentek.testingsettings"

                android:targetClass="com.ragentek.testingsettings.TestingSub1"/>

    </PreferenceScreen>

  

</PreferenceScreen>

布局文件:testing_sub1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/testingSub1"

    />

</LinearLayout>

布局文件:testing_sub2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/testingSub2"

    />

</LinearLayout>

TestingSettings.java类,用addPreferencesFromResource(R.layout.testing_settings)

这个方法引入布局文件:

public class TestingSettings extends PreferenceActivity {

    /** Calledwhen the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.layout.testing_settings);

    }

}

TestingSub.java类,没什么好说的:

public class TestingSub extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.testing_sub1);

    }

 

}

TestingSub1.java类,没什么好说的:

public class TestingSub1 extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.testing_sub2);

    }

 

}

效果图:

主界面:

 

点第一个选项进入下面的页面:


原创粉丝点击