1.Activity的建立,配置和使用

来源:互联网 发布:淘宝发票抬头哪里设置 编辑:程序博客网 时间:2024/06/05 03:01

一、Activity 功能:1.从一个界面跳转到另一个界面,传送Activity所携带的数据,或接收Activity的数据

二、 用法  1、掌握其如何跳转到下一个Activity ,此处 ,单指 Intent 最普通的跳转下一个Activity界面方法

三、注意点: 代码注释  

  主界面源代码:

package com.example.preferenceactivityapi;


import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.app.LauncherActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ArrayAdapter;


public class MainActivity extends LauncherActivity {                         //继承 LuncherActivity ,必须覆写 Intent  intentForPosition(int   position·)方法
    String[] names={
    "查看设置参数",                                                                        //设置ListAdapter呈现的列表名称,设置主界面显示的数字
    "查看星际兵种"
    };
    Class<?>[] clazzs={                                                                                                          //由于每个选项单机后都是Activity页面,需要Intent作为转接跳板,此处定义两个activity类
     PreferenceActivityTest.class,     //PreferenceActivity子类,显示设置选项参数并进行保存的窗口
     ExpandableListActivityTest.class   //是ExpandableListActivity的子类,可扩展的列表窗口
    };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,names);
setListAdapter(adapter);     //简单设置ListAcitivity的设置                                 //   ArrayAdapter的应用,将需要显示的文字显示出来,无需 Layout界面
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public Intent intentForPosition(int position){      

                  / /定义LancherActivity,单击后呈现的Intent,必须覆盖此方法
return new Intent(MainActivity.this,clazzs[position]);           //        当用户点击文字后 ,跳转其指向的Activity界面
}
}  

  跳转ExpandableListActivityTest.class 界面代码,此处代码解析略:

 package com.example.preferenceactivityapi;


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.Gallery.LayoutParams;
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.yecun1,
    R.drawable.yecun2,
    R.drawable.yecun3
    };
    private String[] armTypes=new String[]{
    "神族兵种","虫族兵种","人族兵种"
    };
    private String[][] arms=new String[][]{
    {"狂战士","龙骑士","黑暗圣堂","电兵"},
    {"小狗","刺蛇","飞龙","自爆飞机"},
    {"机枪兵","护士MM","幽灵"}
    };
@Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return arms[arg0][arg1];
}


@Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return arg1;
}
private TextView getTextView(){
@SuppressWarnings("deprecation")
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_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;
}
@Override
public View getChildView(int arg0, int arg1, boolean arg2,
View arg3, ViewGroup arg4) {
// TODO Auto-generated method stub
TextView textView=getTextView();
textView.setText(getChild(arg0,arg1).toString());
return textView;
}


@Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return arms[arg0].length;
}


@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return armTypes[arg0];
}


@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return armTypes.length;
}


@Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}


@Override
public View getGroupView(int groupPosition, boolean arg1, View arg2,
ViewGroup arg3) {
// 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;
}


@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}


@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
     };
     setListAdapter(adapter);
      }
}

配置 Activity  ,如果界面跳转另一个界面时,记得一定要配置Activity ,之后的Intent 一样  ,Manifest 内的 Application内配置Activity,Intent属性都在Activity内配置 ,

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"           
    package="com.example.preferenceactivityapi"             
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.preferenceactivityapi.MainActivity"               //主界面Activity
            android:label="@string/app_name" >   
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="ExpandableListActivityTest"                           //  跳转ExpandableListActivity界面的Activity ,内部可以定义Intent  等各种属性,见 Intent 详解
            android:label="查看星际兵种"
            ></activity>
        <activity  android:name="PreferenceActivityTest"                     //    跳转参数设置Activity
            android:label="设置程序参数"
            ></activity>
    </application>


</manifest>

PreferenceActivityTest界面布局代码:  此代码可添加后续章节的Fragment  注意点:

<?xml version="1.0" encoding="utf-8"?>                                    //设置 Preference界面布局
<preference-headers
    xmlns:android="http://schemas.android.com/apk/res/android">                 
     <header android:fragment="com.example.preferenceactivityapi.PreferenceActivityTest$Prefs1Fragment"                         //Preference与Fragment复合使用,fragment为包名+   fragment类名   ,下方为fragment 定义
         android:icon="@drawable/yecun1"
         android:title="程序设置"
         android:summary="设置应用的相关选项"/>
     <header android:fragment="com.example.preferenceactivityapi.PreferenceActivityTest$Prefs2Fragment"    //一致
         android:icon="@drawable/yecun2"
         android:title="界面选项设置"
         android:summary="设置显示界面的相关选项">
         <extra android:name="website"
             android:value="www.crazyit.org"/>
         </header>
      <header
          android:icon="@drawable/yecun3" 
          android:title="百度"
          android:summary="使用Intent启动某个Activity">
          <intent android:action="android.intent.action.VIEW"
                 android:data="http://www.baidu.com"/>                 //此处为Intent 的Data属性,Data的一般应用
         </header>
         </preference-headers>



设置 Preference内部需要改动的参数 ,掌握如何创建此Preference参数,1.Opens a wizard to help create a new Android XML File ,第四个按钮  2.  选择Preference  ,并选择根元素为PreferenceScreen,单机Finish  ,而后通过Add 添加自己需要的参数,在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="选择铃声"
            android:showDefault="true"
            android:key="ring_key"
            android:showSilent="true" >
            </RingtonePreference>
        
        <PreferenceCategory  android:title="个人信息设置组">
            <EditTextPreference 
                android:key="name"
                android:title="填写用户名"
                android:summary="填写您的用户名"
                android:dialogTitle="您所使用的用户名为:"/>
            <ListPreference 
                android:key="gendar"
                android:title="性别"
                android:summary="选择您的性别"
                android:dialogTitle="ListPreference"
                android:entries="@array/gendar_name_list"
                android:entryValues="@array/gendar_value_list"
                />
        </PreferenceCategory>
        <PreferenceCategory
            android:title="系统功能设置组">
              <CheckBoxPreference 
                  android:key="autoSave"
                  android:title="自动保存进度"
                  android:summaryOn="自动保存:开启"
                  android:summaryOff="自动保存:关闭"
                  android:defaultValue="true"/>
        </PreferenceCategory>
</PreferenceScreen>

PreferenceActivityTest 界面代码 :

package com.example.preferenceactivityapi;


import java.util.List;


import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.widget.Button;
import android.widget.Toast;


public class PreferenceActivityTest extends PreferenceActivity{
      protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
          if(hasHeaders()){
         Button button=new Button(this);
         button.setText("设置操作");
         setListFooter(button);
          }
      }
      public void onBuildHeaders(List<Header> target){
     loadHeadersFromResource(R.layout.preference_headers, target);
      }
      public static class Prefs1Fragment extends PreferenceFragment{
     public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     addPreferencesFromResource(R.xml.preferences);                 //  此 Fragment 为加载   Preference 布局 参数   addPreferenceFromResource()加载参数布局界面,自动保存设置。
     }
      }
public static class Prefs2Fragment extends PreferenceFragment{
     public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     addPreferencesFromResource(R.xml.display_prefs);
     String website=getArguments().getString("website");                                  
     Toast.makeText(getActivity(), "网站域名为:"+website, Toast.LENGTH_LONG).show();
     }
      }
}

  

0 0
原创粉丝点击