当前位置:   article > 正文

Android开发一个简单的交互式App_怎么把做好的交互生成app

怎么把做好的交互生成app
1、功能分析
  • Layout定义App外观
    • 下拉列表列出编程语言的特点
    • 点击按钮,开始处理
    • 文本框显示推荐的编程语言
  • 字符存储在strings.xml
  • activity定义App与用户的交互方式
    • 根据用户在下拉列表的选择,在文本框中返回编程语言
  • 定制的Java程序中包含业务逻辑
    • 存储并输出语言与特点的对应关系
2、开发视图布局
  • activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:layout_editor_absoluteX="186dp"
            tools:layout_editor_absoluteY="366dp">
    
            <Spinner
                android:id="@+id/feature"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:entries="@array/feature"
                android:minHeight="32dp" />
    
            <Button
                android:id="@+id/find_language"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onClickButton"
                android:text="@string/find_language" />
    
            <TextView
                android:id="@+id/language"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/language" />
    
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • strings.xml

    <resources>
        <string name="app_name">ProgramAdviser</string>
        <string name="find_language">Find Language</string>
        <string name="language">Select and Click</string>
        <string-array name="feature">
            <item>fast</item>
            <item>easy</item>
            <item>new</item>
            <item>OO</item>
        </string-array>
    </resources>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 预览

3、按钮事件响应
  • MainActivity类

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void onClickButton(View button){
            //获得Spinner引用
            Spinner spinner = findViewById(R.id.feature);
            //获得Spinner选项
            String feature = spinner.getSelectedItem().toString();
            //获得TextView引用
            TextView textView = findViewById(R.id.language);
            //设置TextView文字
            textView.setText(feature);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
4、开发模型层
  • ProgramExpert类,和MainActivity同级

    public class ProgramExpert {
        public String getLangunage(String feature){
            String result;
            switch (feature){
                case "fast":
                    result = "C/C++";
                    break;
                case "easy":
                    result = "Python";
                    break;
                case "new":
                    result = "Kotlin";
                    break;
                case "OO" :
                    result = "Java";
                    break;
                default:
                    result = "You got me";
            }
            return result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 修改MainActivity类

    package szst.it.ping.programadviser;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Spinner;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        //private封装实例变量,final保证expert被初始化
        //final的实例变量赋值后无法改变,声明后必须立即初始化
        private final ProgramExpert expert = new ProgramExpert();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void onClickButton(View button){
            //获得Spinner引用
            Spinner spinner = findViewById(R.id.feature);
            //获得Spinner选项
            String feature = spinner.getSelectedItem().toString();
    
            //查询模型层
            String langunage = expert.getLangunage(feature);
    
            //获得TextView引用
            TextView textView = findViewById(R.id.language);
            //设置TextView文字
            //textView.setText(feature);
            textView.setText(langunage);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
5、测试结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/267562
推荐阅读
相关标签