package com.example.gridview;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.*;
import android.view.Menu;
public class MainActivity extends Activity {
TextView tv;
GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(ll);
tv = new TextView(this);
tv.setText("你的生日是那一個月。");
gv = new GridView(this);
String[] str = {"一月","二月","三月",
"四月","五月","六月",
"七月","八月","九月",
"十月","十一月","十二月"};
ArrayAdapter<String> ad = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, str);
gv.setAdapter(ad);
gv.setNumColumns(3);
ll.addView(tv);
ll.addView(gv);
gv.setOnItemClickListener(new SampleItemClickListener());
}
class SampleItemClickListener implements OnItemClickListener
{
public void onItemClick(AdapterView<?>v, View iv, int pos, long id)
{
TextView tmp = (TextView) iv;
tv.setText("你的生日是" + tmp.getText());
}
}
@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;
}
}
留言列表