zoukankan      html  css  js  c++  java
  • 黑马程序员-LauncherActivity和preferenceActivity修正版

    LauncherActivity是一种系统提供的采用列表的形式呈现数据,大家可能在前面已经知道Android还提供另一个列表的界面是ListActivity,而LauncherActivity而言最大的不 同是每个项选择不是一个值而是Intent,下面的实例就是创建一个LauncherActivity intentForPosition重写这个接口实现选择相应的项后并启动对应的Activity

    1

    package org.crayit.activity;

    import android.app.ExpandableListActivity;

    import android.app.LauncherActivity;

    import android.content.Intent;

    import android.os.Bundle;

    import android.preference.PreferenceActivity;

    import android.widget.Adapter;

    import android.widget.ArrayAdapter;

    public class OtherActivity extends LauncherActivity {

    String[] names={"设置程序参数","查看星际兵种"};

    Class<?>[] clazzs={PreferenceActivityTest.class,ExpandableListActivityTest.class};

    @Override

    protected void onCreate(Bundle icicle) {

    // TODO Auto-generated method stub

    super.onCreate(icicle);

    ArrayAdapter<String> adater=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,names);

    setListAdapter(adater);

    }

    @Override

    protected Intent intentForPosition(int position) {

    // TODO Auto-generated method stub

    return new Intent(OtherActivity.this,clazzs[position]);

    }

    }

    ExpandableListActivity继承这个接口就可以实现一个可伸缩的列表窗口

    2

    实现代码

    package org.crayit.activity;

    import android.app.ExpandableListActivity;

    import android.os.Bundle;

    import android.view.Gravity;

    import android.view.View;

    import android.view.ViewGroup;

    import android.widget.AbsListView;

    import android.widget.BaseExpandableListAdapter;

    import android.widget.ExpandableListAdapter;

    import android.widget.ImageView;

    import android.widget.LinearLayout;

    import android.widget.TextView;

    public class ExpandableListActivityTest extends ExpandableListActivity{

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    ExpandableListAdapter adapter=new BaseExpandableListAdapter() {

    int[] logos=new int[]{

    R.drawable.p,

    R.drawable.z,

    R.drawable.t

    };

    private String[] armTypes=new String[]

    {"神族兵种","虫族兵种","人族兵种"};

    private String[][] arms=new String[][]

    {

    {"狂战士","龙战士","黑暗圣堂","电兵"},

    {"小狗","刺蛇","飞龙","自爆飞机"},

    {"机枪兵","护士MM","幽灵"}

    };

    private TextView getTextView(){

    AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64);

    TextView textview=new TextView(ExpandableListActivityTest.this);

    textview.setLayoutParams(lp);

    textview.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);

    textview.setPadding(36, 0, 0, 0);

    textview.setTextSize(20);

    return textview;

    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {

    // TODO Auto-generated method stub

    return true;

    }

    public boolean hasStableIds() {

    // TODO Auto-generated method stub

    return true;

    }

    public View getGroupView(int groupPosition, boolean isExpanded,

    View convertView, ViewGroup parent) {

    // TODO Auto-generated method stub

    LinearLayout l1=new LinearLayout(ExpandableListActivityTest.this);

    l1.setOrientation(0);

    ImageView logo=new ImageView(ExpandableListActivityTest.this);

    logo.setImageResource(logos[groupPosition]);

    l1.addView(logo);

    TextView textview=getTextView();

    textview.setText(getGroup(groupPosition).toString());

    l1.addView(textview);

    return l1;

    }

    public long getGroupId(int groupPosition) {

    // TODO Auto-generated method stub

    return groupPosition;

    }

    public int getGroupCount() {

    // TODO Auto-generated method stub

    return armTypes.length;

    }

    public Object getGroup(int groupPosition) {

    // TODO Auto-generated method stub

    return armTypes[groupPosition];

    }

    public int getChildrenCount(int groupPosition) {

    // TODO Auto-generated method stub

    return arms[groupPosition].length;

    }

    public View getChildView(int groupPosition, int childPosition,

    boolean isLastChild, View convertView, ViewGroup parent) {

    // TODO Auto-generated method stub

    TextView textview=getTextView();

    textview.setText(getChild(groupPosition, childPosition).toString());

    return textview;

    }

    public long getChildId(int groupPosition, int childPosition) {

    // TODO Auto-generated method stub

    return childPosition;

    }

    //通过指定组位置、指定子列表项处的子列表数据项

    public Object getChild(int groupPosition, int childPosition) {

    // TODO Auto-generated method stub

    return arms[groupPosition][childPosition];

    }

    };

    setListAdapter(adapter);

    }

    }

    PreferenceActivity是一个很有用的基类,我们在开发的过程难免要对一些参数进行设置,那么这个方法会将选项采用参数的形式保存,保存的目录在data\data\包名\shared_prefs

    图 运行界面

    image

    image

    根据选择生成相对应的xml文件

    <?xml version="1.0" encoding="utf-8" standalone="yes" ?>

    - <map>

    <boolean name="autoSave" value="true" />

    <string name="name">wenchangshou</string>

    </map>

    实现代码:

    package org.crayit.activity;

    import android.os.Bundle;

    import android.preference.PreferenceActivity;

    public class PreferenceActivityTest extends PreferenceActivity {

    /* (non-Javadoc)

    * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)

    */

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);

    }

    }

    package org.crayit.activity;

    import android.os.Bundle;

    import android.preference.PreferenceActivity;

    public class PreferenceActivityTest extends PreferenceActivity {

    /* (non-Javadoc)

    * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)

    */

    @Override

    protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preferences);

    }

    }

    界面的配置主要是在XML中进行设置,那么在设置之前需要新建一个

    在Resource Type:中选择preference

    <?xml version="1.0" encoding="utf-8"?>

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

    <RingtonePreference

    android:title="设置铃声"

    android:summary="选择铃声(测试(Ringtonepreference)"

    android:showDefault="true"

    android:key="ring_key"

    android:showSilent="true"

    />

    <PreferenceCategory

    android:title="个人信息设置zu"

    >

    <EditTextPreference android:title="填写用户名" android:key="name" android:dialogTitle="您使用的用户名为:" android:summary="填写你的用户名(测试EditTextPreference)"/>

    <ListPreference android:summary="选择您的性别(测试Listpreeference)" android:key="gender" android:title="性别" android:dialogTitle="ListPreference" android:entries="@array/gender_name_list" android:entryValues="@array/gender_value_list"/>

    </PreferenceCategory>

    <PreferenceCategory android:title="系统功能设置组">

    <CheckBoxPreference android:summaryOn="自动保存:开启" android:key="autoSave" android:defaultValue="true" android:summaryOff="自动保存:关闭" android:title="自动保存进度"/>

    </PreferenceCategory>

    </PreferenceScreen>

  • 相关阅读:
    函数的一些应用
    关于javascript的一些知识以及循环
    <记录学习>京东页面最后一天HTML以及css遇到的问题
    <记录学习>(前三天)京东页面各种注意点
    银行账号输入格式代码
    CSS兼容性常见问题总结
    移动端实现摇一摇并振动
    LESS使用方法简介(装逼神器)
    H5移动端性能优化
    BFC,IFC,GFC,FFC的定义及功能
  • 原文地址:https://www.cnblogs.com/tianyake/p/2358588.html
Copyright © 2011-2022 走看看