由于是测试查找和排序算法,因此先要有一个目标数组。为了获得一个目标数组,我设置一个
EditText
和一个Button
来添加数据android
<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这个属性合理的分配空间。最后的结果以下数组
由于要用到以前实现的查找排序算法和一些相关的类,因此先把他们从IEDA中复制到Android项目里来ide
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中,我这里以二叉树排序为例3d
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); } });