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> -
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> -
預覽

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、測驗結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/302816.html
標籤:其他
