Android开发一个简单的交互式App

1、功能分析

  • Layout定义App外观
    • 下拉列表列出编程语言的特点
    • 点击按钮,开始处理
    • 文本框显示推荐的编程语言
  • 字符存储在strings.xml
  • activity定义App与用户的交互方式
    • 根据用户在下拉列表的选择,在文本框中返回编程语言
  • 定制的Java程序中包含业务逻辑
    • 存储并输出语言与特点的对应关系

2、开发视图布局

  • activity_main.xml

    
    <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>
    
  • strings.xml

    <resources>
        <string name="app_name">ProgramAdviserstring>
        <string name="find_language">Find Languagestring>
        <string name="language">Select and Clickstring>
        <string-array name="feature">
            <item>fastitem>
            <item>easyitem>
            <item>newitem>
            <item>OOitem>
        string-array>
    resources>
    
  • 预览

    Android开发一个简单的交互式App_第1张图片

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);
        }
    }
    

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;
        }
    }
    
  • 修改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);
        }
    }
    

5、测试结果

Android开发一个简单的交互式App_第2张图片

你可能感兴趣的:(Android学习笔记,android)