Android学习08-----Android中的基本控件(下)(5)_GridView,AnalogClock与DigitalClock,Chronomete

 

一、网格视图: GridView

1 SimpleAdapter 实现

SimpleAdapterActivity.java

package com.iflytek.demo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;

public class SimpleAdapterActivity extends Activity {
	private GridView myGirdView = null; // 取得组件
	private SimpleAdapter simpleAdapter = null;
	private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.initAdapter(); // 实例化SimpleAdapter类对象
		this.myGirdView = (GridView) super.findViewById(R.id.myGridView);
		this.myGirdView.setAdapter(this.simpleAdapter);
		this.myGirdView.setOnItemClickListener(new OnItemClickListenerImpl());
	}

	private class OnItemClickListenerImpl implements OnItemClickListener {

		@SuppressWarnings("unchecked")
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			ImageView showImg = new ImageView(SimpleAdapterActivity.this);
			showImg.setScaleType(ImageView.ScaleType.CENTER); // 图片居中显示
			showImg.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			Map<String, Integer> map = (Map<String, Integer>) parent
					.getAdapter().getItem(position);
			showImg.setImageResource(map.get("img")); // 设置显示图片
			Dialog dialog = new AlertDialog.Builder(SimpleAdapterActivity.this)
					.setIcon(R.drawable.ic_launcher)
					.setTitle("查看图片")
					.setView(showImg)
					.setNegativeButton("关闭",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub

								}
							}).create();
			dialog.show();
		}

	}

	private void initAdapter() {
		Field[] fields = R.drawable.class.getDeclaredFields();
		for (int x = 0; x < fields.length; x++) {
			if (fields[x].getName().startsWith("png_")) {
				Map<String, Integer> map = new HashMap<String, Integer>();
				try {
					map.put("img", fields[x].getInt(R.drawable.class));
				} catch (Exception e) {
				}
				this.list.add(map);
			}
		}
		this.simpleAdapter = new SimpleAdapter(this, this.list,
				R.layout.grid_layout, new String[] { "img" },
				new int[] { R.id.img });
	}
}

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">
	<GridView
		android:id="@+id/myGridView"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:numColumns="3"
		android:stretchMode="columnWidth" />
</LinearLayout>

grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" 
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:background="#000000">
	<ImageView
		android:id="@+id/img"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:scaleType="center"
		android:padding="3px" />
</LinearLayout>

2 BaseAdapter

BaseAdapterActivity.java

package com.iflytek.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class BaseAdapterActivity extends Activity {
	private GridView myGirdView = null; // 取得组件
	private int[] picRes = new int[] { R.drawable.png_01, R.drawable.png_02,
			R.drawable.png_03, R.drawable.png_04, R.drawable.png_05,
			R.drawable.png_06, R.drawable.png_07, R.drawable.png_08,
			R.drawable.png_09, R.drawable.png_10, R.drawable.png_11,
			R.drawable.png_12, R.drawable.png_13, R.drawable.png_14,
			R.drawable.png_15, R.drawable.png_16, R.drawable.png_17,
			R.drawable.png_18, R.drawable.png_19, R.drawable.png_20,
			R.drawable.png_21, R.drawable.png_22, R.drawable.png_23,
			R.drawable.png_24 };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.myGirdView = (GridView) super.findViewById(R.id.myGridView);
		this.myGirdView.setAdapter(new ImageAdapter(this, this.picRes));
		this.myGirdView.setOnItemClickListener(new OnItemClickListenerImpl());
	}

	private class OnItemClickListenerImpl implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			ImageView showImg = new ImageView(BaseAdapterActivity.this);
			showImg.setScaleType(ImageView.ScaleType.CENTER); // 图片居中显示
			showImg.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			showImg.setImageResource(BaseAdapterActivity.this.picRes[position]); // 设置显示图片
			Dialog dialog = new AlertDialog.Builder(BaseAdapterActivity.this)
					.setIcon(R.drawable.ic_launcher)
					.setTitle("查看图片")
					.setView(showImg)
					.setNegativeButton("关闭",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub

								}
							}).create();
			dialog.show();
		}

	}
}

ImageAdapter.java

package com.iflytek.demo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
	private Context context = null;
	private int picIds[] = null;

	public ImageAdapter(Context context, int picIds[]) {
		this.context = context;
		this.picIds = picIds;
	}

	@Override
	public int getCount() {
		return this.picIds.length;
	}

	@Override
	public Object getItem(int position) {
		return this.picIds[position];
	}

	@Override
	public long getItemId(int position) {
		return this.picIds[position];
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView img = new ImageView(this.context);
		img.setImageResource(this.picIds[position]);
		img.setScaleType(ImageView.ScaleType.CENTER);
		return img;
	}

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/myGridView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="3"
        android:stretchMode="columnWidth" />

</LinearLayout>

 

二、时钟组件: AnalogClock DigitalClock

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">
	<AnalogClock 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
	<DigitalClock
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
</LinearLayout>

 

 

三、计时器: Chronometer

1 、控制计时

Chronometer02_Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

public class Chronometer02_Activity extends Activity {
	private Chronometer myChronometer = null;
	private Button butStart = null;
	private Button butStop = null;
	private Button butBase = null;
	private Button butFormat = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.myChronometer = (Chronometer) super
				.findViewById(R.id.myChronometer);
		this.butStart = (Button) super.findViewById(R.id.butStart);
		this.butStop = (Button) super.findViewById(R.id.butStop);
		this.butBase = (Button) super.findViewById(R.id.butBase);
		this.butFormat = (Button) super.findViewById(R.id.butFormat);
		this.butStart.setOnClickListener(new OnClickListenerImplStart());
		this.butStop.setOnClickListener(new OnClickListenerImplStop());
		this.butBase.setOnClickListener(new OnClickListenerImplBase());
		this.butFormat.setOnClickListener(new OnClickListenerImplFormat());
	}

	private class OnClickListenerImplStart implements OnClickListener {

		@Override
		public void onClick(View v) {
			Chronometer02_Activity.this.myChronometer.start(); // 开始计时
		}

	}

	private class OnClickListenerImplStop implements OnClickListener {

		@Override
		public void onClick(View v) {
			Chronometer02_Activity.this.myChronometer.stop(); // 停止计时
		}

	}

	private class OnClickListenerImplBase implements OnClickListener {

		@Override
		public void onClick(View v) {
			Chronometer02_Activity.this.myChronometer.setBase(SystemClock
					.elapsedRealtime()); // 开始计时
		}

	}

	private class OnClickListenerImplFormat implements OnClickListener {

		@Override
		public void onClick(View v) {
			Chronometer02_Activity.this.myChronometer.setFormat("新的显示格式:%s。");
		}

	}
}

 

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">
	<Chronometer
		android:id="@+id/myChronometer"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" 
		android:layout_width="fill_parent"
		android:layout_height="fill_parent">
		<Button
			android:id="@+id/butStart"
			android:text="开始计时"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<Button
			android:id="@+id/butStop"
			android:text="停止计时"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<Button
			android:id="@+id/butBase"
			android:text="复位"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<Button
			android:id="@+id/butFormat"
			android:text="格式化"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
	</LinearLayout>
</LinearLayout>

2 、定时震动

Chronometer03_Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;

public class Chronometer03_Activity extends Activity {
	private Chronometer myChronometer = null;
	private Button butStart = null;
	private Button butStop = null;
	private Vibrator vibrator = null; // 震动操作

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.vibrator = (Vibrator) super.getApplication().getSystemService(
				Service.VIBRATOR_SERVICE); // 取得震动服务

		this.myChronometer = (Chronometer) super
				.findViewById(R.id.myChronometer);
		this.butStart = (Button) super.findViewById(R.id.butStart);
		this.butStop = (Button) super.findViewById(R.id.butStop);

		this.myChronometer.setFormat("当前计时时间:%s。");

		this.myChronometer
				.setOnChronometerTickListener(new OnChronometerTickListenerImpl());

		this.butStart.setOnClickListener(new OnClickListenerImplStart());
		this.butStop.setOnClickListener(new OnClickListenerImplStop());
	}

	private class OnChronometerTickListenerImpl implements
			OnChronometerTickListener {

		@Override
		public void onChronometerTick(Chronometer chronometer) {
			String time = chronometer.getText().toString()
					.replaceAll("[^(\\d{2}:\\d{2})]", "");
			Log.i("Chronometer03_Activity", time);
			if ("01:00".equals(time)) {
				Chronometer03_Activity.this.vibrator.vibrate(new long[] { 1000,
						10, 100, 100 }, 0); // 重复震动
			}
		}
	}

	private class OnClickListenerImplStart implements OnClickListener {

		@Override
		public void onClick(View v) {
			Chronometer03_Activity.this.myChronometer.start(); // 开始计时
		}

	}

	private class OnClickListenerImplStop implements OnClickListener {

		@Override
		public void onClick(View v) {
			Chronometer03_Activity.this.myChronometer.stop(); // 停止计时
		}

	}
}

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">
	<Chronometer
		android:id="@+id/myChronometer"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:orientation="horizontal" 
		android:layout_width="fill_parent"
		android:layout_height="fill_parent">
		<Button
			android:id="@+id/butStart"
			android:text="开始计时"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" />
		<Button
			android:id="@+id/butStop"
			android:text="停止计时"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" /> 
	</LinearLayout>
</LinearLayout>

 

四、标签: TabHost

1 、通过继承 TabActivity

TabHost01_Activity.java

package com.iflytek.demo;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabHost01_Activity extends TabActivity { // 继承了TabActivity
	private TabHost myTabHost;
	private int[] layRes = new int[] { R.id.tab_edit, R.id.tab_clock,
			R.id.tab_sex }; // 这些是内嵌布局文件的ID

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.myTabHost = super.getTabHost(); // 取得TabHost对象
		LayoutInflater.from(this).inflate(R.layout.tab,
				this.myTabHost.getTabContentView(), true); // true表示实例化布局文件中的组件
		for (int x = 0; x < this.layRes.length; x++) {
			TabSpec myTab = this.myTabHost.newTabSpec("tab" + x);
			myTab.setIndicator("标签  - " + x);
			myTab.setContent(this.layRes[x]);
			this.myTabHost.addTab(myTab);
		}
	}
}

tab.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">
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:id="@+id/tab_edit"
		android:orientation="vertical" 
		android:layout_width="fill_parent"
		android:layout_height="fill_parent">
		<EditText
			android:id="@+id/edit" 
			android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:text="请输入检索关键字..."
			android:textSize="18px" />
		<Button
			android:id="@+id/but"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="搜索" />
	</LinearLayout>
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:id="@+id/tab_clock"
		android:orientation="vertical" 
		android:layout_width="fill_parent"
		android:layout_height="fill_parent">
		<AnalogClock
			android:id="@+id/myAnalogClock" 
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"/>
	</LinearLayout>
	<LinearLayout 
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:id="@+id/tab_sex"
		android:orientation="vertical" 
		android:layout_width="fill_parent"
		android:layout_height="fill_parent">
		<RadioGroup
			android:id="@+id/sex" 
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:orientation="vertical">
			<RadioButton
				android:id="@+id/male"
				android:checked="true"
				android:text="性别:男" />
			<RadioButton
				android:id="@+id/female"
				android:text="性别:女" />
		</RadioGroup>
	</LinearLayout>
</LinearLayout>

2 、继承 Activity

TabHost02_Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabHost02_Activity extends Activity { // 继承了TabActivity
	private TabHost myTabHost;
	private int[] layRes = new int[] { R.id.tab_edit, R.id.tab_clock,
			R.id.tab_sex }; // 这些是内嵌布局文件的ID

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.tab);
		this.myTabHost = (TabHost) super.findViewById(R.id.tabhost);
		this.myTabHost.setup(); // 建立TabHost对象
		for (int x = 0; x < this.layRes.length; x++) {
			TabSpec myTab = this.myTabHost.newTabSpec("tab" + x);
			myTab.setIndicator("标签  - " + x);
			myTab.setContent(this.layRes[x]);
			this.myTabHost.addTab(myTab);
		}
		this.myTabHost.setCurrentTab(0); // 默认显示的标签索引
	}
}

3 、标签在下方

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabHost03_Activity extends Activity {
	private TabHost myTabHost;
	private int[] layRes = new int[] { R.id.tab_edit, R.id.tab_clock,
			R.id.tab_sex }; // 这些是内嵌布局文件的ID

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.tab);
		this.myTabHost = (TabHost) super.findViewById(R.id.tabhost);
		this.myTabHost.setup(); // 建立TabHost对象
		for (int x = 0; x < this.layRes.length; x++) {
			TabSpec myTab = this.myTabHost.newTabSpec("tab" + x);
			myTab.setIndicator("标签  - " + x);
			myTab.setContent(this.layRes[x]);
			this.myTabHost.addTab(myTab);
		}
		this.myTabHost.setCurrentTab(0); // 默认显示的标签索引
	}
}

 

 

 

你可能感兴趣的:(GridView,tabhost,Chronometer,DigitalClock,AnalogClock)