Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android Gridview生成程序快捷鍵的簡單方法

android Gridview生成程序快捷鍵的簡單方法

編輯:關於Android編程

mainpage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
   
 <GridView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/gridview"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
    android:padding="10dp" 
    android:verticalSpacing="10dp" 
     
    android:horizontalSpacing="10dp" 
    android:numColumns="auto_fit" 
    android:columnWidth="60dp" 
    android:stretchMode="columnWidth" 
     
    android:gravity="center" 
    />   
 
   
     
 </LinearLayout>
icon_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  
        xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_height="wrap_content"  
         android:paddingBottom="4dip" android:layout_width="fill_parent"> 
        <ImageView  
              android:layout_height="65dp"  
              android:layout_width="65dp"
              android:id="@+id/ItemImage"    
              android:layout_centerHorizontal="true">  
        </ImageView> 
       <TextView  
              android:layout_width="wrap_content"  
              android:layout_below="@+id/ItemImage"  
              android:layout_height="wrap_content"  
              android:text="TextView01"  
              android:layout_centerHorizontal="true"  
              android:id="@+id/ItemText"> 
        </TextView> 
</RelativeLayout> 

 


MainPageActivity

package Android.ERP;

import java.util.ArrayList;
import java.util.HashMap;


import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.GridView;
import android.widget.SimpleAdapter;

public class MainPageActivity extends Activity{
   
 
   
     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainpage);
         createShutCut();
       

    }
    //生成快捷鍵
    void createShutCut()
    {
          GridView gridview = (GridView) findViewById(R.id.gridview); 

        //生成動態數組,並且轉入數據 
        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>(); 
        lstImageItem.add(getIcon("我的通訊錄", R.drawable.inco_1)); 
        lstImageItem.add(getIcon("我的課表", R.drawable.inco_5)); 
   
    
        lstImageItem.add(getIcon("退出", R.drawable.inco_4)); 

        //生成適配器的ImageItem <====> 動態數組的元素,兩者一一對應 
        SimpleAdapter saImageItems = new SimpleAdapter(this, //沒什麼解釋 
                                                  lstImageItem,//數據來源  
                                                      R.layout.icon_item,//night_item的XML實現 
                                                  
                                                     //動態數組與ImageItem對應的子項         
                                                    new String[] {"ItemImage","ItemText"},  
                                                     
                                                    //ImageItem的XML文件裡面的一個ImageView,兩個TextView ID 
                                                  new int[] {R.id.ItemImage,R.id.ItemText}); 
           //添加並且顯示 
           gridview.setAdapter(saImageItems); 
         //添加消息處理 
           gridview.setOnItemClickListener(new ItemClickListener());
       
    }
    HashMap<String, Object>    getIcon(String IconTitle,int drawableIconID)
    {
       
        HashMap<String, Object> map = new HashMap<String, Object>(); 
        map.put("ItemImage", drawableIconID);//添加圖像資源的ID 
        
        map.put("ItemText",IconTitle);//按序號做ItemText
  
        return  map;
    }
   
    class  ItemClickListener implements OnItemClickListener 
      { 
    public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened  
                                     View arg1,//The view within the AdapterView that was clicked 
                               int arg2,//The position of the view in the adapter 
                                    long arg3//The row id of the item that was clicked 
                               ) { 
      //arg2=arg3 
       HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2); 
       //顯示所選Item的ItemText 
       setTitle((String)item.get("ItemText"));
   
      String  title  =(String)item.get("ItemText");
      switchMoudel(title);

    } 
     //進入指定模塊
     void switchMoudel(String strTitle)
     {
        
         if(strTitle.equalsIgnoreCase("我的課表"))
                 enterMoudel(MyClassListActivity.class );
         if(strTitle.equalsIgnoreCase("我的通訊錄"))
             enterMoudel( ContactBookListActivity.class );
            
      
      if (strTitle.equalsIgnoreCase("退出"))
      {
          Log.d("activity count:", activityList.size()+"");
          QuitSystem();
         
      }
        
        
        
        
     }
    
     //進入指定模塊 strTitle:模塊名稱,enterActivityName:進入的activityh名稱
       void enterMoudel(Class<?> enterActivityName   )
     {
        
          Intent intent=new Intent(MainPageActivity.this,enterActivityName) ;
          startActivity(intent);
        
     }
    
   
     }
   
   
   
  
   
   
     
}
作者:guozh

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved