用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

GridView 实例

2012-10-28 作者: 神马举报

[android]代码库

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;

/**
 * GridView 实例
 */
public class EX04_18 extends Activity {
	private TextView mTextView01;
	private Button mButton01, mButton02;
	private GridView mGridView01;
	private String[] mGames1, mGames2;
	private ArrayAdapter aryAdapter1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		/* 4个字符串数组 */
		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) };
		/* 9个字符串数组 */
		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) };
		mButton01 = (Button) findViewById(R.id.myButton1);
		mButton02 = (Button) findViewById(R.id.myButton2);
		mGridView01 = (GridView) findViewById(R.id.myGridView1);
		mTextView01 = (TextView) findViewById(R.id.myTextView1);
		mButton01.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/* 4个元素,以2栏方式呈现(2x2) */
				mGridView01.setNumColumns(2);
				aryAdapter1 = new ArrayAdapter(EX04_18.this,
						R.layout.simple_list_item_1_small, mGames1);
				mGridView01.setAdapter(aryAdapter1);
				// mGridView01.setScrollBarStyle(DEFAULT_KEYS_DIALER);
				mGridView01.setSelection(2);
				mGridView01.refreshDrawableState();
			}
		});
		mButton02.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/* 9个元素,以3栏方式呈现(3x3) */
				mGridView01.setNumColumns(3);
				aryAdapter1 = new ArrayAdapter(EX04_18.this,
						R.layout.simple_list_item_1_small, mGames2);
				mGridView01.setAdapter(aryAdapter1);
			}
		});
		mGridView01.setOnItemClickListener(new GridView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView parent, View v, int position,
					long arg3) {
				// TODO Auto-generated method stub
				/* 判断Adapter里的元素个数,判断被点选的是第几个元素名称 */
				switch (aryAdapter1.getCount()) {
				case 4:
					/* position为GridView里的元素索引值 */
					mTextView01.setText(mGames1[position]);
					break;
				case 9:
					mTextView01.setText(mGames2[position]);
					break;
				}
			}
		});
	}
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...