欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-03 12:46:01
...

 

一、缩放控制: 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();
	}

}