【Android】Activity类及其子类:LauncherActivity、PreferenceActivity和ExpandableListActivity
2014-11-06 21:29
441 查看
Activity类及其子类类图:
下面的例子展示了LauncherActivity、PreferenceActivity和ExpandableListActivity的简单用法。
LauncherActivityTest.java:
PreferenceActivityTest.java:
ExpandableListActivityTest.java:
expandable_group.xml:
点击参数设置:
剩余部分:
点击选择产品:
下面的例子展示了LauncherActivity、PreferenceActivity和ExpandableListActivity的简单用法。
LauncherActivityTest.java:
package com.zzj.ui.activitydemo; import android.app.LauncherActivity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; import android.widget.ArrayAdapter; public class LauncherActivityTest extends LauncherActivity { private String[] data = { "参数设置", "选择产品" }; private Class<?>[] classes = { PreferenceActivityTest.class, ExpandableListActivityTest.class }; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); setListAdapter(adapter); } @Override protected Intent intentForPosition(int position) { return new Intent(this, classes[position]); } /** * 获取PreferenceActivity设置的参数 */ @Override protected void onResume() { super.onResume(); SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this); boolean flyMode = sharedPreferences.getBoolean("flyMode", false); String editText = sharedPreferences.getString("editText1", ""); String wlanSleep = sharedPreferences.getString("wlanSleep", ""); boolean keyDownVoice = sharedPreferences.getBoolean("keyDownVoice", false); String ringtone = sharedPreferences.getString("ringtone", ""); String alarm = sharedPreferences.getString("alarm", ""); String notification = sharedPreferences.getString("notification", ""); String all = sharedPreferences.getString("all", ""); Log.d("flyMode", String.valueOf(flyMode)); Log.d("editText1", editText); Log.d("wlanSleep", wlanSleep); Log.d("keyDownVoice", String.valueOf(keyDownVoice)); Log.d("ringtone", ringtone); Log.d("alarm", alarm); Log.d("notification", notification); Log.d("all", all); } }LauncherActivity不需要布局文件。重写intentForPosition(int position)方法,点击每个列表项的时候,会回调该方法,通过返回的Intent跳转至Activity。
PreferenceActivityTest.java:
package com.zzj.ui.activitydemo; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; import android.util.Log; import com.zzj.ui.R; public class PreferenceActivityTest extends PreferenceActivity { @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 该方法已过时 addPreferencesFromResource(R.xml.preference_activity); // 该方法已过时 CheckBoxPreference checkBoxPreference = (CheckBoxPreference) findPreference("flyMode"); /* * 事件监听 * * Change事件会在Click事件之前触发 */ checkBoxPreference .setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { Log.d("ClickListener_checked", String .valueOf(((CheckBoxPreference) preference) .isChecked())); return false;// 事件是否结束 } }); checkBoxPreference .setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { Log.d("ChangeListener_checked", newValue.toString()); // 该方法是在状态更改前调用,如果返回false,状态不会更新。 return true; } }); } }PreferenceActivity布局文件:res/xml/preference_activity.xml,
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:title="无线和网络" > <CheckBoxPreference android:key="flyMode" android:summary="禁用所有无线连接" android:title="飞行模式" /> <CheckBoxPreference android:disableDependentsState="false" android:key="wlan" android:summary="打开WLAN" android:title="WLAN" /> <CheckBoxPreference android:dependency="wlan" android:key="staticIP" android:summary="静态IP" android:title="静态IP" /> <ListPreference android:defaultValue="sleepWhenScreenOff" android:dependency="wlan" android:dialogTitle="WLAN休眠政策" android:entries="@array/wlan_sleep_text" android:entryValues="@array/wlan_sleep_value" android:key="wlanSleep" android:negativeButtonText="取消" android:summary="指定合适从WLAN切换至移动数据" android:title="WLAN休眠政策" /> <CheckBoxPreference android:key="bluetooth" android:summary="打开蓝牙" android:title="蓝牙" /> </PreferenceCategory> <PreferenceCategory android:title="可编辑选项" > <EditTextPreference android:dialogIcon="@drawable/ic_launcher" android:dialogMessage="dialogMessage" android:dialogTitle="dialogTitle" android:key="editText1" android:negativeButtonText="取消" android:positiveButtonText="确定" android:summary="editText1editText1" android:title="editText1" /> </PreferenceCategory> <PreferenceCategory android:title="声音设置" > <SwitchPreference android:key="keyDownVoice" android:title="按键声" /> <RingtonePreference android:key="ringtone" android:ringtoneType="ringtone" android:title="铃声" /> <RingtonePreference android:key="alarm" android:ringtoneType="alarm" android:title="闹铃" /> <RingtonePreference android:key="notification" android:ringtoneType="notification" android:title="通知" /> <RingtonePreference android:key="all" android:ringtoneType="all" android:title="所有" /> </PreferenceCategory> </PreferenceScreen>preference提供的基本组件有:CheckBoxPreference、EditTextPreference、ListPreference、RingtonePreference以及SwitchPreference。也可以定义自己的preference。
ExpandableListActivityTest.java:
package com.zzj.ui.activitydemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; import android.widget.Toast; import com.zzj.ui.R; public class ExpandableListActivityTest extends ExpandableListActivity { private String[] names = { "腾讯", "百度", "阿里巴巴" }; private String[][] childnames = { { "QQ", "微信", "手机卫士" }, { "百度地图", "百度视频", "PPS&奇艺" }, { "支付宝", "新浪微博", "高德地图" } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>(); for (int i = 0; i < names.length; i++) { Map<String, String> map = new HashMap<String, String>(); map.put("name", names[i]); groupData.add(map); String[] childs = childnames[i]; List<Map<String, String>> list = new ArrayList<Map<String, String>>(); for (int j = 0; j < childs.length; j++) { Map<String, String> childMap = new HashMap<String, String>(); childMap.put("childname", childs[j]); list.add(childMap); } childData.add(list); } SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData, R.layout.expandable_group, new String[] { "name" }, new int[] { R.id.expandable_group_textview }, childData, R.layout.expandable_child, new String[] { "childname" }, new int[] { R.id.expandable_child_textview }); setListAdapter(adapter); } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { String text = names[groupPosition] + "\r\n" + childnames[groupPosition][childPosition]; Toast.makeText(this, text, Toast.LENGTH_LONG).show(); return true; } }ExpandableListActivity也不需要布局文件,但是可以定义父列表布局文件和子列表布局文件。
expandable_group.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/expandable_group_textview" android:layout_marginLeft="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>expandable_child.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/expandable_child_textview" android:layout_marginLeft="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>效果图:
点击参数设置:
剩余部分:
点击选择产品:
相关文章推荐
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
- 第八课:Activity 及其子类的使用二:ExpandableListActivity
- Android LauncherActivity,ExpandableListActivity,PreferenceActivity
- Android上 ExpandableListActivity及CursorTreeAdapter的使用简介
- Android开发--浅谈ExpandableListActivity
- Android Activity 及其子类
- Android基础之ExpandableListActivity
- LauncherActivity,PreferenceActivity,ExpandableListActivity
- Android开发视频第二季之三:ExpandableListActivity和SimpleExpandableListAdapter的使用
- android ExpandableListActivity简介
- Android UI ExpandableListActivity与ExpandableListActivity
- android 学习笔记(1) ExpandableListActivity 自定义view
- [Android实例] 发现ExpandableListActivity的onChildClick方法失效
- android学习--ExpandableListActivity和SimpleExpandableListAdapter的使用
- [Android] ExpandableListActivity使用findViewById查找child中view的时序问题
- marsAndroid学习——常见控件ExpandableListActivityh和SimpleExpandableListAdapter的简单使用
- Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView
- (android 基础知识) ExpandableListActivity
- Android ExpandableListActivity 学习笔记
- Android基础之ExpandableListActivity