20162311 编写Android程序测试查找排序算法
20162311 编写Android程序测试查找排序算法
一、设置图形界面
因为是测试查找和排序算法,所以先要有一个目标数组。为了得到一个目标数组,我设置一个
EditText和一个Button来添加数据
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<EditText
android:id="@+id/addData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入要添加的数据"></EditText>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加数据" />
<TextView
android:id="@+id/dataArray"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
采用垂直线性布局,EditText添加一个hint属性,提示用户添加数据,然后点击按钮即可把数据添加到数组中,后面的TextView用来显示数组中的元素。接下来又是一个EditText,用来让客户输入要查找的对象;之后是两个按钮,一个是查找,点击之后可以在目标数组中进行查找,一个是排序,点击之后会对目标数组进行排序,并把排序结果显示在后面的TextView中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="@+id/target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入要查找的目标"></EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/search"
android:layout_width="193dp"
android:layout_height="wrap_content"
android:text="查找" />
<Button
android:id="@+id/sort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="排序" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/searchText"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint=""/>
<TextView
android:id="@+id/sortText"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint=""/>
</LinearLayout>
</LinearLayout>
整体的布局采用了线性布局,其中又嵌套了线性布局,利用weight这个属性合理的分配空间。最后的结果如下

二、编写MainActivity
因为要用到之前实现的查找排序算法和一些相关的类,所以先把他们从IEDA中复制到Android项目里来

- 设置变量,找到对应控件的id
private EditText addData,target;
private TextView dataArray,searchText,sortText;
private Button add,search,sort;
addData = (EditText) findViewById(R.id.addData);
target = (EditText) findViewById(R.id.target);
dataArray = (TextView)findViewById(R.id.dataArray);
searchText = (TextView)findViewById(R.id.searchText);
sortText = (TextView)findViewById(R.id.sortText);
add = (Button)findViewById(R.id.add);
search = (Button)findViewById(R.id.search);
sort = (Button)findViewById(R.id.sort);
- 给对应的按钮设置监听器
首先是add按钮,它的功能是把第一个EditText中输入的数据存入数组中,然后把数组中的数据显示在下面的TextView中。其中start是一个整型变量,初始值为0,每添加一个元素自加一
add.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Integer dataText = Integer.getInteger(addData.getText().toString());
data[start] = dataText;
start++;
String result = "";
for(int i:data)
result += i+" ";
dataArray.setText(result);
}
});
然后设置search按钮,它的功能是接收第二个EditText中的数据,作为查找目标,然后调用查找方法进行查找,并把结果显示在下面的TextView中。我这里是以数表查找为例
search.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
int targetext = Integer.getInteger(target.getText().toString());
int result = NewSearching.treeSearch(data,targetext);
searchText.setText("查找结果为:"+ result);
}
});
最后是sort按钮,用来对目标数组进行排序,然后把排序结果显示在下面的TextView中,我这里以二叉树排序为例
sort.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
NewSorting.binaryTreeSort(data);
String result = "排序结果:\n";
for (int s: data)
result += s+"\n";
sortText.setText(result);
}
});
三、测试结果截图

20162311 编写Android程序测试查找排序算法的更多相关文章
- 用Eclipse编写Android程序的代码提示功能
用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示 Window->Preferences- ...
- Visual Studio 开始支持编写 Android 程序并自带 Android 模拟器【转载】
原文地址 本文内容 为什么需要一个 Android 模拟器 针对 Visual Studio Android 模拟器的调试 Visual Studio Android 模拟器的传感器模拟和其他功能 A ...
- Android程序测试
一.建立测试环境 安装了Android Developer Tools (ADT) 插件的Eclipse将为你创建,构建,以及运行Android程序提供一个基于图形界面的集成开发环境.Eclipse的 ...
- java小程序整理及排序算法
1. 利用循环打印如下图形 ***** **** *** ** * public class Main { public static void main(String[] args) { // TO ...
- 剑指offer 查找和排序的基本操作:查找排序算法大集合
重点 查找算法着重掌握:顺序查找.二分查找.哈希表查找.二叉排序树查找. 排序算法着重掌握:冒泡排序.插入排序.归并排序.快速排序. 顺序查找 算法说明 顺序查找适合于存储结构为顺序存储或链接存储的线 ...
- c#(控制台应用程序)实现排序算法的研究总结
前言:闲来无事,便研究起来对数组的排序算法,怕过后遗忘,特地总结一下,也希望能帮到大家 概要: 总结的算法: 冒泡排序.插入排序.选择排序 要排序的一列数(从小到大): 1, 5, 3, 83, 4 ...
- [转]eclipse下编写android程序突然不会自动生成R.java文件和包的解决办法
原网址 : http://www.cnblogs.com/zdz8207/archive/2012/11/30/eclipse-android-adt-update.html 网上解决方法主要有这几种 ...
- 用Xamarin + VS 编写Android程序体验及其与Android Studio的比较
昨天看了微软2016Build大会,Xamarin免费了.恩,5亿美刀的家伙,哈哈,我也要体验一下..... 1. 首先在Xamarin官网下载安向导:https://www.xamarin.com/ ...
- 用eclipse编写Android程序时怎样生成apk文件
转载请注明出处:http://blog.csdn.net/ns_code/article/details/16828449 APK是Android Package的缩写,即Android安装包.通过将 ...
随机推荐
- Linux:file命令显示自定义文件类型
file 命令可以查看文件类型信息,原理见: 非常Linux-file命令与magic file 修改 /ect/magic 文件后,可用 file 命令显示自定义文件类型信息. man magic ...
- Linux 工具,一本好书 大牛的博客
http://linuxtools-rst.readthedocs.io/zh_CN/latest/base/index.html http://design-patterns.readthedocs ...
- Mybatis 代码自动生成[myeclipse版]
使用环境说明: OS:windows 7 64位 myeclipse: 2017 CI 1.安装 打开myeclipse--help---Install from catalog--选择eclipse ...
- HDU5033 building 单调栈+计算几何
正解:单调栈 解题报告: 哇生气辽QAQ本来打了半天feel good都快调出来了然后说换题了QAQ(所以可能那题的代码会过一阵子再放上来了QAQ 不过还是大爆手速打了一通拿到首杀了嘻嘻 美滋滋辽 然 ...
- (3.11)mysql基础深入——mysql文件分类与配置文件管理
(3.11)mysql基础深入——mysql文件分类与管理 关键词:mysql配置文件,mysql参数文件,mysql中的my.cnf 目录:mysql数据库文件分类: [1]参数文件:my.cnf ...
- 解决 libev.so.4()(64bit) is needed by percona-xtrabackup-2.3.4-1.el6.x86_64案例
在mysql主从同步时经常会用到Xtra, XtraBackup可以说是一个相对完美的免费开源数据备份工具,支持在线无锁表同步复制和可并行高效率的安全备份恢复机制相比mysqldump来说优势较大好处 ...
- 关于Ubuntu中Could not get lock /var/lib/dpkg/lock解决方案
在Ubuntu中,有时候运用sudo apt-get install 安装软件时,会出现一下的情况 E: Could not get lock /var/lib/dpkg/lock - open ( ...
- 使用SolrJ代码导入,发布搜索服务
搭建solr服务器:http://www.cnblogs.com/liyafei/p/8005571.html 一导入要搜索的字段 1:确定发布搜索的字段,sql语句 SELECT a.id, b. ...
- Session实例
Session常用方法(一) session对象用来保存一些在与每个用户回话期间需要保存的数据信息,这样就方便了回话期间的一些处理程序.如可以用session变量记住用户的用户名,以后就不必在其他的网 ...
- Tomcat重启session失效
在Tomcat的目录下找到context.xml,取消掉<Manager pathname="" /> 这句的注释.