`

Android学习08-----Android中的基本控件(下)(7)_ZoomControls,PopupWindow,ExpandableListView

阅读更多

 

一、缩放控制: ZoomControls

Mian.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" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好"
        android:textSize="10px" />

    <ZoomControls
        android:id="@+id/zommcontrols"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom" />

</LinearLayout>
 

 

ZoomControlsActivity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ZoomControls;

public class ZoomControlsActivity extends Activity {
	private ZoomControls zoomcontrols = null;
	private TextView text = null;
	private int size = 10; // 是保存文字大小

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.zoomcontrols = (ZoomControls) super
				.findViewById(R.id.zommcontrols);
		this.text = (TextView) super.findViewById(R.id.text);

		this.zoomcontrols
				.setOnZoomInClickListener(new OnZoomInClickListenerImpl());
		this.zoomcontrols
				.setOnZoomOutClickListener(new OnZoomOutClickListenerImpl());
	}

	private class OnZoomInClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			ZoomControlsActivity.this.size = size + 2;
			ZoomControlsActivity.this.text.setTextSize(size);
		}

	}

	private class OnZoomOutClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			ZoomControlsActivity.this.size = size - 2;
			ZoomControlsActivity.this.text.setTextSize(size);
		}

	}
}
 

 

二、弹出窗口: PopupWindow

PopupWindowActivity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class PopupWindowActivity extends Activity {
	private Button popbut = null;
	private TextView statusinfo = null;
	private View popView = null;
	private PopupWindow popWin = null;
	private RadioGroup changestatus = null;
	private Button cancel = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.popbut = (Button) super.findViewById(R.id.popbut);
		this.statusinfo = (TextView) super.findViewById(R.id.statusinfo);

		this.popbut.setOnClickListener(new OnClickListenerImpl());
	}

	private class OnClickListenerImpl implements OnClickListener {

		@Override
		public void onClick(View v) {
			LayoutInflater inflater = LayoutInflater
					.from(PopupWindowActivity.this);
			PopupWindowActivity.this.popView = inflater.inflate(
					R.layout.popwindow, null); // 找到了布局文件中的View
			PopupWindowActivity.this.popWin = new PopupWindow(
					PopupWindowActivity.this.popView, 300, 220, true);
			PopupWindowActivity.this.changestatus = (RadioGroup) PopupWindowActivity.this.popView
					.findViewById(R.id.changestatus); // 取得弹出界面中的组件
			PopupWindowActivity.this.cancel = (Button) PopupWindowActivity.this.popView
					.findViewById(R.id.cancel);
			PopupWindowActivity.this.changestatus
					.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl());
			PopupWindowActivity.this.cancel
					.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							PopupWindowActivity.this.popWin.dismiss(); // 不显示
						}
					});
			PopupWindowActivity.this.popWin.showAtLocation(
					PopupWindowActivity.this.popbut, Gravity.CENTER, 0, 0);
		}
	}

	private class OnCheckedChangeListenerImpl implements
			OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			RadioButton but = (RadioButton) PopupWindowActivity.this.popView
					.findViewById(group.getCheckedRadioButtonId()); // 取得指定的单选钮被选中
			PopupWindowActivity.this.statusinfo.setText("当前用户状态:"
					+ but.getText().toString());
			PopupWindowActivity.this.popWin.dismiss();
		}

	}
}
 

 

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/statusinfo"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="当前用户状态:在线" />
	<Button 
		android:id="@+id/popbut"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="状态" />
</LinearLayout>

 

 

popwindow.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/popinfo"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:text="请选择您的当前状态:"
		android:textSize="20px" />
	<RadioGroup
		android:id="@+id/changestatus"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:orientation="vertical"
		android:checkedButton="@+id/online">
		<RadioButton
			android:id="@+id/online"
			android:text="在线" />
		<RadioButton
			android:id="@+id/offline"
			android:text="离线" />
		<RadioButton
			android:id="@+id/stealth"
			android:text="隐身" />
	</RadioGroup>
	<Button 
		android:id="@+id/cancel"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" 
		android:text="取消"/>
</LinearLayout>

 

三、树型组件: ExpandableListView

1 、组项监听

ExpandableListView02_Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class ExpandableListView02_Activity extends Activity {
	private ExpandableListView elistview = null;
	private ExpandableListAdapter adapter = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.elistview = (ExpandableListView) super
				.findViewById(R.id.elistview);
		this.adapter = new MyExpandableListAdapter(this);
		this.elistview.setAdapter(this.adapter);

		this.elistview.setOnChildClickListener(new OnChildClickListenerImpl());
		this.elistview.setOnGroupClickListener(new OnGroupClickListenerImpl());
		this.elistview
				.setOnGroupExpandListener(new OnGroupExpandListenerImpl());
		this.elistview
				.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());
	}

	private class OnChildClickListenerImpl implements OnChildClickListener {

		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			Toast.makeText(
					ExpandableListView02_Activity.this,
					"子选项被选中,groupPosition = " + groupPosition
							+ ",childPosition = " + childPosition,
					Toast.LENGTH_SHORT).show();
			return false;
		}
	}

	private class OnGroupClickListenerImpl implements OnGroupClickListener {

		@Override
		public boolean onGroupClick(ExpandableListView parent, View v,
				int groupPosition, long id) {
			Toast.makeText(ExpandableListView02_Activity.this,
					"分组被选中,groupPosition = " + groupPosition,
					Toast.LENGTH_SHORT).show();
			return false;
		}

	}

	private class OnGroupExpandListenerImpl implements OnGroupExpandListener {

		@Override
		public void onGroupExpand(int groupPosition) {
			Toast.makeText(ExpandableListView02_Activity.this,
					"打开分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)
					.show();

		}

	}

	private class OnGroupCollapseListenerImpl implements
			OnGroupCollapseListener {

		@Override
		public void onGroupCollapse(int groupPosition) {
			Toast.makeText(ExpandableListView02_Activity.this,
					"关闭分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)
					.show();

		}

	}
}
 

 

MyExpandableListAdapter.java

package com.iflytek.demo;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
	// 一个组下肯定会有多个子选项
	private String[] groups = new String[] { "我的好友", "家人", "同事", "黑名单" }; // 组名称
	private String[][] children = new String[][] { { "赵一", "吴二", "张三", "李四" },
			{ "父亲", "母亲" }, { "王五", "赵六", "王八" }, { "票贩子", "造假商" } }; // 这里面定义的子选项
	private Context context = null;

	public MyExpandableListAdapter(Context context) {
		this.context = context;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) { // 取得指定的子选项
		return this.children[groupPosition][childPosition];
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	private TextView buildTextView() {
		AbsListView.LayoutParams params = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 55);
		TextView textView = new TextView(this.context);
		textView.setLayoutParams(params);
		textView.setTextSize(15.0f);
		textView.setGravity(Gravity.LEFT);
		textView.setPadding(70, 8, 3, 3);
		return textView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		TextView textView = this.buildTextView();
		textView.setText(this.getChild(groupPosition, childPosition).toString());
		return textView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return this.children[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return this.groups[groupPosition];
	}

	@Override
	public int getGroupCount() {
		return this.groups.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView textView = this.buildTextView();
		textView.setText(this.getGroup(groupPosition).toString());
		return textView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}

 

 

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" >

    <ExpandableListView
        android:id="@+id/elistview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
 

 

3 、上下文菜单

ExpandableListView03Activity.java

package com.iflytek.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

public class ExpandableListView03Activity extends Activity {
	private ExpandableListView elistview = null;
	private ExpandableListAdapter adapter = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		this.elistview = (ExpandableListView) super
				.findViewById(R.id.elistview);
		this.adapter = new MyExpandableListAdapter(this);
		this.elistview.setAdapter(this.adapter);

		super.registerForContextMenu(this.elistview);

		this.elistview.setOnChildClickListener(new OnChildClickListenerImpl());
		this.elistview.setOnGroupClickListener(new OnGroupClickListenerImpl());
		this.elistview
				.setOnGroupExpandListener(new OnGroupExpandListenerImpl());
		this.elistview
				.setOnGroupCollapseListener(new OnGroupCollapseListenerImpl());
	}

	private class OnChildClickListenerImpl implements OnChildClickListener {

		@Override
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			Toast.makeText(
					ExpandableListView03Activity.this,
					"子选项被选中,groupPosition = " + groupPosition
							+ ",childPosition = " + childPosition,
					Toast.LENGTH_SHORT).show();
			return false;
		}
	}

	private class OnGroupClickListenerImpl implements OnGroupClickListener {

		@Override
		public boolean onGroupClick(ExpandableListView parent, View v,
				int groupPosition, long id) {
			Toast.makeText(ExpandableListView03Activity.this,
					"分组被选中,groupPosition = " + groupPosition,
					Toast.LENGTH_SHORT).show();
			return false;
		}

	}

	private class OnGroupExpandListenerImpl implements OnGroupExpandListener {

		@Override
		public void onGroupExpand(int groupPosition) {
			Toast.makeText(ExpandableListView03Activity.this,
					"打开分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)
					.show();

		}

	}

	private class OnGroupCollapseListenerImpl implements
			OnGroupCollapseListener {

		@Override
		public void onGroupCollapse(int groupPosition) {
			Toast.makeText(ExpandableListView03Activity.this,
					"关闭分组,groupPosition = " + groupPosition, Toast.LENGTH_SHORT)
					.show();

		}

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
		int type = ExpandableListView
				.getPackedPositionType(info.packedPosition); // 取得菜单项
		int group = ExpandableListView
				.getPackedPositionGroup(info.packedPosition); // 取得所在组的索引
		int child = ExpandableListView
				.getPackedPositionChild(info.packedPosition); // 取得子菜单项的索引
		Toast.makeText(ExpandableListView03Activity.this,
				"type = " + type + ",group = " + group + ",child = " + child,
				Toast.LENGTH_SHORT).show();
	}

}
分享到:
评论

相关推荐

    Android开发教程之Android控件--千锋培训

    在Android开发中,控件是构建用户界面的基本元素,它们为用户提供交互操作的方式。这篇教程主要涵盖了九种常见的Android控件及其使用示例,包括ZoomControls、Include、VideoView、WebView、RatingBar、Tab、Spinner...

    智能家居系统 缩放控件ZoomControls.doc

    在大多数编程环境中,ZoomControls通常是一个派生自基础控件的类,例如Android中的ZoomControls继承自ViewGroup。这样的设计允许开发者利用已有的控件属性和方法,同时添加特定于缩放功能的特性。在学习ZoomControls...

    Android编程实现图片放大缩小功能ZoomControls控件用法实例

    首先,我们需要在布局文件(如activity_main.xml)中添加ZoomControls控件和ImageView控件,如下所示: ```xml ... android:id="@+id/layout"&gt; android:id="@+id/image" ... /&gt; &lt;ZoomControls android:...

    ZoomControls 缩放

    一般应用开发过程中,会在屏幕的右下角有两个缩放控件(缩小和放大),这个控件就是ZoomControls 如何使用其实现放大缩小功能: 1:首先在布局文件中配置ZoomControls控件 &lt;ZoomControls android:id="@+id/...

    Android控件及布局的使用

    ### Android控件及布局的使用知识点详解 #### 第一章:Android基础 ...通过以上知识点的学习,开发者可以更深入地理解Android控件及布局的应用,并能够熟练地在自己的应用中使用这些控件来提升用户体验。

    移动互联网-缩放控件ZoomControls.ppt

    移动互联网应用

    Android_UI类继承关系图

    Android UI类继承关系图描述了Android开发中,用户界面组件之间的层次结构和继承关系。在Android开发中,UI组件构建在一种称为View的类之上,View又是Object的子类,构成了一个庞大的类体系。通过继承关系图,开发者...

    Android2.2 API中文文档——View

    在Android开发中,`View`是最基本的UI组件,所有可见的用户界面元素都是通过继承`View`类实现的。`View`类定义了控件的行为和外观,并且提供了绘制、布局、处理触摸事件等功能。本文档将详细介绍Android2.2版本中的`...

    ZoomControls放大与缩小

    ZoomControls控件是一个可以缩放的控件,以下是它但一些主要但方法 hasFocus ():判断焦点 hide ():隐藏 onTouchEvent (MotionEvent event):现这个方法来处理触摸屏移动事件 setIsZoomInEnabled (boolean is...

    android缩放组件源码

    在这个项目中,我们可能会看到`ZoomControls`类的实现,这是一个预定义的Android控件,用于提供简单的放大和缩小功能。`ZoomControls`通常用于地图应用或其他需要用户进行缩放操作的场景。 源码分析: 1. **...

    Android 将 android view 的位置设为右下角的解决方法

    默认情况下,我们在eclipse中拖动控件到editor中,控件的位置将位于整个屏幕的左上角。可以使用 android:layout_gravity、android:gravity和android:width三个属性值,实现控件的九宫格定位。  Xml代码 代码如下:...

    ArcGIS Runtime SDK for Android开发基础

    在深入探讨ArcGIS Runtime SDK for Android的开发之前,我们首先需要理解GIS的基本概念。 GIS(Geographic Information System)是一种整合了地图、地理数据和计算能力的系统,用于捕捉、存储、管理、分析和展示...

    Android-GPS定位与Google地图管理模块的代码设计.pdf

    【Android-GPS定位与Google地图管理模块的代码设计】这篇技术文档主要讲解了如何在Android平台上构建基于GPS定位和Google地图的导航系统。系统开发采用了Java语言和Eclipse开发工具,涉及的关键技术包括GPS定位、...

    Android地图使用文档

    `MapView`是Android SDK中用于展示地图的控件,它是`MapView`类的实例,继承自`SurfaceView`。在`actMap`类中,我们看到`MapView`被初始化并添加到布局中。通过`findViewById()`方法获取到这个组件,并用它来设置...

    指尖疯Android4.4视频第5集:图像类系列组件

    4. ZoomControls控件。 ImageView/ImageButton控件属于非常简单的控件,我们就从这两个入手开始。剩余的控件和手机等移动设备关系比较紧密,我们随后看看基于移动设备有什么新特性的控件。本集的口号是“我的面子我...

    Android开发规范

    在Android开发中,遵循一套规范是非常重要的,它能提高代码的可读性、可维护性和团队协作效率。以下是一些关键的Android开发规范和注意事项: **布局方面:** 1. **优先使用线性布局(LinearLayout)**,因为它简单...

    Android 类似于放大镜源码.zip

    通过研究这个`ZoomControls`类的源码,开发者可以学习到如何在Android上实现一个交互式的放大镜功能,这对于提高用户体验和开发生活类应用非常有帮助。同时,这也是一个很好的实践案例,可以帮助开发者加深对Android...

    Android Map

    在Android平台上,地图功能是应用开发中的重要组成部分,它允许用户查看地理位置、导航以及执行其他与位置相关的操作。本文将详细探讨"Android Map"的相关知识点,包括地图的显示、缩放、位置查询以及如何实现个人...

Global site tag (gtag.js) - Google Analytics