Android练习之GridView

code

 

View Code
  1 package com.example.flexd9;
  2 
  3 import android.os.Bundle;
  4 import android.app.Activity;
  5 import android.view.Menu;
  6 import android.view.View;
  7 import android.view.View.OnClickListener;
  8 import android.widget.AdapterView;
  9 import android.widget.AdapterView.OnItemClickListener;
 10 import android.widget.ArrayAdapter;
 11 import android.widget.Button;
 12 import android.widget.GridView;
 13 import android.widget.TextView;
 14 
 15 /**
 16  * 点击Button在GridView显示相应阵列,点击阵列内容在TextView显示相应选中值
 17  */
 18 public class MainActivity extends Activity {
 19     
 20     private TextView tv1;
 21     private GridView gv1;
 22     private Button bt1,bt2;
 23     private String[] strArray1,strArray2;
 24     private ArrayAdapter arrayAdapter1;
 25 
 26     @Override
 27     protected void onCreate(Bundle savedInstanceState) {
 28         super.onCreate(savedInstanceState);
 29         setContentView(R.layout.activity_main);
 30         
 31         //4个字符串阵列 目标是2X2
 32         strArray1 = new String[]{
 33                 getResources().getString(R.string.str_list1),
 34                 getResources().getString(R.string.str_list2),
 35                 getResources().getString(R.string.str_list3),
 36                 getResources().getString(R.string.str_list4)
 37         };
 38         
 39         //9个字符串阵列 目标是3X3
 40         strArray2 = new String[]{
 41                 getResources().getString(R.string.str_list1),
 42                 getResources().getString(R.string.str_list2),
 43                 getResources().getString(R.string.str_list3),
 44                 getResources().getString(R.string.str_list4),
 45                 getResources().getString(R.string.str_list5),
 46                 getResources().getString(R.string.str_list6),
 47                 getResources().getString(R.string.str_list7),
 48                 getResources().getString(R.string.str_list8),
 49                 getResources().getString(R.string.str_list9),
 50         };
 51         
 52         // Widget
 53         tv1 = (TextView) findViewById(R.id.tv1);
 54         gv1 = (GridView) findViewById(R.id.gv1);
 55         bt1 = (Button) findViewById(R.id.bt1);
 56         bt2 = (Button) findViewById(R.id.bt2);
 57         
 58         /**
 59          * listener 点击Button给GridView赋不同阵列值
 60          */
 61         OnClickListener listener = new OnClickListener() {
 62             @Override
 63             public void onClick(View v) {
 64                 //2X2
 65                 if(v.getId()==R.id.bt1){
 66                     arrayAdapter1 = new ArrayAdapter(MainActivity.this,R.layout.list_item,strArray1);
 67                     gv1.setNumColumns(2);
 68                     gv1.setAdapter(arrayAdapter1);    
 69                     gv1.setSelection(2);
 70                     gv1.refreshDrawableState();
 71                 //3X3
 72                 }else if(v.getId()==R.id.bt2){
 73                     arrayAdapter1 = new ArrayAdapter(MainActivity.this,R.layout.list_item,strArray2);
 74                     gv1.setNumColumns(3);
 75                     gv1.setAdapter(arrayAdapter1);
 76                 }
 77             }
 78         };
 79         
 80         bt1.setOnClickListener(listener);
 81         bt2.setOnClickListener(listener);
 82         
 83         /**
 84          * 点击GridView给TextView赋值相应内容
 85          */
 86         OnItemClickListener itemListener = new OnItemClickListener(){
 87 
 88             @Override
 89             public void onItemClick(AdapterView arg0, View arg1, int arg2,
 90                     long arg3) {
 91                 switch(arrayAdapter1.getCount()){
 92                 //arg2是position
 93                     case 4:
 94                         tv1.setText(strArray1[arg2]);
 95                         break;
 96                     case 9:
 97                         tv1.setText(strArray2[arg2]);
 98                         break;
 99                 }
100             }
101         };
102         
103         gv1.setOnItemClickListener(itemListener);
104     }
105 
106     @Override
107     public boolean onCreateOptionsMenu(Menu menu) {
108         // Inflate the menu; this adds items to the action bar if it is present.
109         getMenuInflater().inflate(R.menu.activity_main, menu);
110         return true;
111     }
112 
113 }

 

转载于:https://www.cnblogs.com/imzzh/archive/2012/12/26/2833665.html

你可能感兴趣的:(Android练习之GridView)