java:
package EX04_18.txt;
import android.R.anim;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
public class EX04_18 extends Activity {
private TextView mTextView;
private Button mButton1, mButton2;
private GridView mGridView;
private String[] mGames1, mGames2;
private ArrayAdapter<String> aryAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initApp();
startApp();
}
private void startApp() {
// TODO Auto-generated method stub
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mGridView.setNumColumns(2);
aryAdapter = new ArrayAdapter<String>(EX04_18.this,
android.R.layout.simple_list_item_1, mGames1);
mGridView.setAdapter(aryAdapter);
mGridView.setSelection(2);
mGridView.refreshDrawableState();
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mGridView.setNumColumns(3);
aryAdapter = new ArrayAdapter<String>(EX04_18.this,
android.R.layout.simple_list_item_1, mGames2);
mGridView.setAdapter(aryAdapter);
mGridView.setSelection(3);
mGridView.refreshDrawableState();
}
});
mGridView.setOnItemClickListener(new GridView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch (aryAdapter.getCount()) {
case 4:
mTextView.setText("你选择的是:"+mGames1[arg2]);
break;
case 9:
mTextView.setText("你选择的是:"+mGames2[arg2]);
break;
default:
mTextView.setText("你没有导入GrideView");
break;
}
}
});
}
private void initApp() {
// TODO Auto-generated method stub
mTextView = (TextView)findViewById(R.id.myTextView);
mButton1 = (Button)findViewById(R.id.myButton1);
mButton2 = (Button)findViewById(R.id.myButton2);
mGridView = (GridView)findViewById(R.id.myGridView);
mGames1 = new String[]
{
getResources().getString(R.string.str_list1),
getResources().getString(R.string.str_list2),
getResources().getString(R.string.str_list3),
getResources().getString(R.string.str_list4)
};
mGames2 = new String[]
{
getResources().getString(R.string.str_list1),
getResources().getString(R.string.str_list2),
getResources().getString(R.string.str_list3),
getResources().getString(R.string.str_list4),
getResources().getString(R.string.str_list1),
getResources().getString(R.string.str_list2),
getResources().getString(R.string.str_list3),
getResources().getString(R.string.str_list4),
getResources().getString(R.string.str_list1)
};
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<GridView
android:id = "@+id/myGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id = "@+id/myButton1"
android:layout_gravity = "center"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text = "mGames1的内容"
/>
<Button
android:id = "@+id/myButton2"
android:layout_gravity = "center"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text = "mGames2的内容"
/>
</LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="hello">Hello World, EX04_18!</string>
<string name="app_name">EX04_18</string>
<string name="str_list1">字符串1</string>
<string name="str_list2">字符串2</string>
<string name="str_list3">字符串3</string>
<string name="str_list4">字符串4</string>
</resources>