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

记录一次android测试实例

程序员文章站 2022-11-21 21:15:09
记录一次android测试实例 /** * 测试进度条 */ public class progresscomponentactivity extends activity { p...

记录一次android测试实例

/**
 * 测试进度条
 */
public class progresscomponentactivity extends activity {
	private linearlayout ll_progress_loading;
	private progressbar pd_progress_loading;
	private seekbar sb_progress_loading;
	private onseekbarchangelistener onseekbarchangelistener = new onseekbarchangelistener() {
		//离开滑杆
		@override
		public void onstoptrackingtouch(seekbar seekbar) {
			log.e("tag", "离开滑杆");
			//1.得到seekbar的进度
			int progress = sb_progress_loading.getprogress();
			//2.设置为progressbar的进度
			pd_progress_loading.setprogress(progress);
			//3.判断是否达到最大值
			if (progress == sb_progress_loading.getmax()) {
				//如果达到最大值,设置圆形进度条不可见
				ll_progress_loading.setvisibility(view.invisible);//可见占用空间
//				ll_progress_loading.setvisibility(view.gone);//不可见占用空间
			}else{
				//如果没有达到的话,设置圆形进度条显示
				ll_progress_loading.setvisibility(view.visible);
			}
		}
		
		@override
		public void onstarttrackingtouch(seekbar seekbar) {
			log.e("tag", "按下滑杆");
		}
		
		@override
		public void onprogresschanged(seekbar seekbar, int progress,
				boolean fromuser) {
			log.e("tag", "滑杆移动");
		}
	};


	@override
	protected void oncreate(bundle savedinstancestate) {
		super.oncreate(savedinstancestate);
		setcontentview(r.layout.activity_progress_component);
		
		ll_progress_loading = (linearlayout) findviewbyid(r.id.ll_progress_loading);
		pd_progress_loading = (progressbar) findviewbyid(r.id.pd_progress_loading);
		sb_progress_loading = (seekbar) findviewbyid(r.id.sb_progress_loading);
		
		//给seekber设置监听
		sb_progress_loading.setonseekbarchangelistener(onseekbarchangelistener );
	}

}





activity_progress_component.xml

<linearlayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <linearlayout
        android:id="@+id/ll_progress_loading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:gravity="center">


        <progressbar
            style="?android:attr/progressbarstylelarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <textview
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正在加载中... ..." />


    </linearlayout>


    <progressbar
        android:id="@+id/pd_progress_loading"
        style="?android:attr/progressbarstylehorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:progress="30"/>


    <seekbar
        android:id="@+id/sb_progress_loading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="功能需求:\n1.滑动下面的滑杆后,上面的进度条会同步
        \n2.滑动到最大值时,最上面的进度条消失" />



</linearlayout>



//菜单menu

menucomponentactivity.java

import android.app.activity;

import android.os.bundle;
import android.view.contextmenu;
import android.view.menu;
import android.view.menuinflater;
import android.view.menuitem;
import android.view.view;
import android.view.contextmenu.contextmenuinfo;
import android.widget.button;
import android.widget.toast;
/**
 * 测试菜单menu
 * optionmenu
 * 1.如何触发menu的显示?
 *点击menu键
 *2.如何向menu中添加menuitem?
 *重写oncreateoptionsmenu方法
 * 1.menu.add()
 * 2.菜单文件
 *3.选择某个menuitem时如何响应?
 * 重写onoptionsitemselected方法
 *
 *
 *
 * 1.如何触发menu的显示?
 *长按某个视图,并view.setoncreatecontextlistener(this)
 *2.如何向menu中添加menuitem?
 *重写oncreatecontextmenu()方法,用menu.add()添加,(菜单文件方式也可以)
 *3.选择某个menuitem时如何响应?
 *重写oncontextitemselected方法,根据itemid响应
 */
public class menucomponentactivity extends activity {
private button btn_menu_cm;


@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_menu_component);

btn_menu_cm = (button) findviewbyid(r.id.btn_menu_cm);
//设置创建上下文菜单的监听
btn_menu_cm.setoncreatecontextmenulistener(this);
}
//用来显示选型菜单的方法,向menu中添加item
@override
public boolean oncreateoptionsmenu(menu menu) {
//纯编码方式
menu.add(0, 2, 0, "添加");
menu.add(0, 3, 0, "删除");
return super.oncreateoptionsmenu(menu);
}


// @override
// public boolean oncreateoptionsmenu(menu menu) {
// //菜单文件的方式
// //1.得到菜单加载器对象
// menuinflater menuinflater = getmenuinflater();
// //2.加载菜单文件
// menuinflater.inflate(r.menu.option_menu, menu);
// return super.oncreateoptionsmenu(menu);
// }


@override
public boolean onoptionsitemselected(menuitem item) {
switch (item.getitemid()) {
case 2:
// case r.id.add:
toast.maketext(this, "添加", toast.length_short).show();
break;
case 3:
// case r.id.add:
toast.maketext(this, "删除", toast.length_short).show();
break;
}
return super.onoptionsitemselected(item);
}


@override
public void oncreatecontextmenu(contextmenu menu, view v,
contextmenuinfo menuinfo) {
super.oncreatecontextmenu(menu, v, menuinfo);
//添加菜单项
menu.add(0,1,0,"添加");
menu.add(0,2,0,"删除");
}

@override
public boolean oncontextitemselected(menuitem item) {
switch (item.getitemid()) {
case 1:
toast.maketext(this, "添加", toast.length_short).show();
break;
case 2:
toast.maketext(this, "删除", toast.length_short).show();
break;
}
return super.oncontextitemselected(item);
}
}






activity_menu_component.xml

<linearlayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <button
        android:id="@+id/btn_menu_cm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/menu_btn" />


    <textview
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1.点击menu显示选项菜单\n2.长按按钮显示上下文菜单" 
        android:textsize="30sp"/>



</linearlayout>



菜单文件方式

option_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="https://schemas.android.com/apk/res/android" >


    <item
        android:id="@+id/add"
        android:title="添加2"/>
    <item
        android:id="@+id/del"
        android:title="删除2"/>


</menu>