2. MainActivity.java
package org.me.android_gridview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.GridView; import android.widget.Toast; public class MainActivity extends Activity { private GridView gridView; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); gridView = (GridView) findViewById(R.id.gridView); final String[] list = new String[]{"王建民","郭泓志","倪福德","胡金龙"}; //设定列表内容 ArrayAdapteradapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,list); //设定列数 gridView.setNumColumns(2); gridView.setAdapter(adapter); //项目被点选时,回应的动作 gridView.setOnItemClickListener(new GridView.OnItemClickListener(){ @Override public void onItemClick(AdapterView adapterView,View view,int position,long id) { Toast.makeText(MainActivity.this, "您选择的是"+list[position], Toast.LENGTH_SHORT).show(); } }); } }
3. main.xml(Layout)
<"
4. 执行之後的画面。
沒有留言:
張貼留言