• 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 = "排序结果:
    ";
                    for (int s: data)
                        result += s+"
    ";
                    sortText.setText(result);
                }
            });
    

    三、测试结果截图

  • 相关阅读:
    Java后端知识体系
    HashMap底层实现整理
    Java线程池
    Spring Boot+Dubbo 入门
    Go 代码记录(一)
    Servlet 复习
    Spring Cloud(二)Eureka:服务注册与发现
    Spring Cloud (一)概述
    数据结构基础知识
    容器技术-Docker入门
  • 原文地址:https://www.cnblogs.com/-zzr-/p/7822622.html
Copyright © 2020-2023  润新知