4 LauncherActivity PreferenceActivity ExpandableActivity

来源:互联网 发布:windows控制面板快捷键 编辑:程序博客网 时间:2024/05/21 06:54

------------------------------------main.java------------------------------------------------


package com.example.qqw;


import android.app.LauncherActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;


public class MainActivity extends LauncherActivity {


//定义两个Activity的名称
String[] names = {"设置程序参数" ,  "查看星际兵种"};
//定义两个Activity对应的实现类
Class<?>[] clazzs = {PreferenceActivityTest.class 
, ExpandableListActivityTest.class};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main); //这句话是可以不用的
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1 , names);
// 设置该窗口显示的列表所需的Adapter
setListAdapter(adapter);
}


//根据列表项来返回指定Activity对应的Intent
@Override 
public Intent intentForPosition(int position)
{
return new Intent(MainActivity.this , clazzs[position]);
}
}


---------------------------------------------PreferenceActivity.java-----------------------


package com.example.qqw;


import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;


public class PreferenceActivityTest extends PreferenceActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置显示参数设置布局。
addPreferencesFromResource(R.xml.preferences);
}



}


-------------------------------------------------------------ExpandableListActivity.java---------------------------------------


package com.example.qqw;
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 {
public void onCreate(Bundle savedInstanceState)
{
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" , "幽灵" }
};
//获取指定组位置、指定子列表项处的子列表项数据

public Object getChild(int groupPosition, int childPosition)
{
return arms[groupPosition][childPosition];
}

public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}

public int getChildrenCount(int groupPosition)
{
return arms[groupPosition].length;
}
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 View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取指定组位置处的组数据

public Object getGroup(int groupPosition)
{
return armTypes[groupPosition];
}

public int getGroupCount()
{
return armTypes.length;
}

public long getGroupId(int groupPosition)
{
return groupPosition;
}
//该方法决定每个组选项的外观

public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(ExpandableListActivityTest.this);
ll.setOrientation(0);
ImageView logo = new ImageView(ExpandableListActivityTest.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}

public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}

public boolean hasStableIds()
{
return true;
}
};
// 设置该窗口显示列表
setListAdapter(adapter);
}
}



。。。。。。。。。。。。。。。。。。。。。main.xml。。。。。。。。。。。。。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/android:list"   
android:layout_width="match_parent"   
android:layout_height="match_parent"   
android:background="#0000ff"   
android:layout_weight="1"   
android:drawSelectorOnTop="false"/>
</LinearLayout>


。。。。。。。。。。。。。。。。。。。。。/res/xml/preference.xml。。。。。。。。。。。。。。。


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置系统铃声 -->
<RingtonePreference android:ringtoneType="all"
android:title="设置铃声" 
android:summary="选择铃声(测试RingtonePreference)"
android:showDefault="true" 
android:key="ring_key"
android:showSilent="true">
    </RingtonePreference>
    <PreferenceCategory android:title="个人信息设置zu">
    <!-- 通过输入框填写用户名 -->
<EditTextPreference
android:key="name"
android:title="填写用户名"
android:summary="填写您的用户名(测试EditTextPreference)"
android:dialogTitle="您所使用的用户名为:"
/>
<!-- 通过列表框选择性别 -->
    <ListPreference
        android:key="gender"
        android:title="性别"
        android:summary="选择您的性别(测试ListPreference)"
        android:dialogTitle="ListPreference"
        android:entries="@array/gender_name_list"
        android:entryValues="@array/gender_value_list"
    />  
</PreferenceCategory>
<PreferenceCategory android:title="系统功能设置组 ">
<CheckBoxPreference
android:key="autoSave"
android:title="自动保存进度"
android:summaryOn="自动保存: 开启"
android:summaryOff="自动保存: 关闭"
android:defaultValue="true"
/>
</PreferenceCategory>
</PreferenceScreen>


。。。。。。。。。。。。。。。/res/values/arrays.xml。。。。。。。。。。。。。。。。。


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="gender_name_list">
<item>男</item>
<item>女</item>
</string-array>
<string-array name="gender_value_list">
<item>male</item>
<item>female</item>
</string-array>
</resources>


、、记得别忘了加:

<activity  android:name=".PreferenceActivityTest" android:label="设置程序参数"/>
        <activity  android:name=".ExpandableListActivityTest" android:label="查看星际兵种"/>

0 0
原创粉丝点击