Android入門(五):程序架構——MVC設計模式在Android中的應用


  剛剛接觸編程的的人,可能會這樣認為:只要代碼寫完了能夠跑起來就算完工了。如果只是寫一個小程序,“能夠跑起來”這樣的標准也就可以了,但是如果你是在公司進行程序的開發,那么僅僅讓程序成功的跑起來是不行的,事情遠沒有你想的這么簡單。一個商業項目的代碼少則數萬行,多則上百萬甚至更多,這種商業項目不可能僅僅靠一個人完成,要想高效高質量的完成開發工作,就需要一個專業的開發團隊了。在團隊中,有人負責項目的架構設計,有些人負責程序代碼的編寫….要想像這樣做到項目開發的分工就必須在程序的結構上做適當的安排。

  舉個例子,大多數商業化軟件都有不同的語言版本,這些不同語言版本的軟件在功能上是完全一樣的,如果我們能夠把軟件上的文字與程序分離開來,這樣就能夠很方便的發布不同語言的版本了。

  MVC全名是Model View Controller,是模型(model)-視圖(view)-控制器(controller)的縮寫,一種軟件設計典范,用一種業務邏輯、數據、界面顯示分離的方法組織代碼,將業務邏輯聚集到一個部件里面,在改進和個性化定制界面及用戶交互的同時,不需要重新編寫業務邏輯。

 

 

  現在,我們在之前的代碼基礎上,進行一些改動,把MVC模式應用的程序當中。

  1.新增一個setupViewCompoent()方法負責運行View相關的所用程序代碼,包括取得接口布局文件中的接口組件和設置接口組件中事件處理程序。

  2.把寫在程序中的字符串放在strings.xml資源文件中,定義在strings.xml資源文件中的字符串在經過編譯后會放到資源類R中,然后程序再從資源類R中取得所需要的字符          串。

  3.在main.xml接口布局文件中,我們把里面的提示文字定義在strings.xml資源文件中,然后再到資源類R中取出字符串使用。代碼如下:

    strings.xml資源文件:

  <resources>
   <string name="app_name">健身咨詢</string>
  <string name="promptSex">性別:</string>
   <string name="promptAge">年齡:</string>
   <string name="promptBtnDoSug">健身咨詢</string>
  <string name="sugResult">結果:</string>
   <string name="sugRun">跑步</string>
   <string name="sugSwim">游泳</string>
   <string name="sugSuggestion">健康咨詢</string>
  <string name="sexMale">男</string>
  </resources>


 
 main.xml文件:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
   android:layout_height="match_parent">

  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/promptSex"/>
<EditText
   android:id="@+id/edtSex"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" />
   android:text=""/>
<TextView
   android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:inputType="text"
   android:text="@string/promptAge"/>
<EditText
  android:id="@+id/edtAge"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:inputType="number"
   android:text=""/>
<Button
   android:id="@+id/btnDoSug"
  android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/promptBtnDoSug"/>
<TextView
   android:id="@+id/txtResult"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/sugResult"/>
</LinearLayout>


修改程序代碼:
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;

  public class MainActivity extends Activity {

   private Button btnDoSug;
   private EditText edtSex, edtAge;
  private TextView txtResult;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   setupViewComponent();
   }

   private void setupViewComponent(){
   //從資源類R中取得接口組件
   btnDoSug = (Button)findViewById(R.id.btnDoSug);
   edtSex = (EditText)findViewById(R.id.edtSex);
  edtAge = (EditText)findViewById(R.id.edtAge);
  txtResult = (TextView)findViewById(R.id.txtResult);
  //button組件事件的listener
   btnDoSug.setOnClickListener(btnDoSugOnClick);
   }

   private Button.OnClickListener btnDoSugOnClick = new Button.OnClickListener(){

   public void onClick(View view){
   //點擊按鈕后執行的代碼
   String strSex = edtSex.getText().toString();
   int iAge = Integer.parseInt(edtAge.getText().toString());

   String strSug = "結果:";
   if(strSex.equals("男"))
   {
   if(iAge < 28)
   strSug += getString(R.string.sugRun);
   else if(iAge > 33)
   strSug += getString(R.string.sugRun);
   else
   strSug += getString(R.string.sugRun);
   }
   else
   {
   if(iAge < 28)
   strSug += getString(R.string.sugRun);
  else if(iAge > 33)
  strSug += getString(R.string.sugSwim);
   else
   strSug += getString(R.string.sugSwim);
   }

   txtResult.setText(strSug);
   }
   };
  }
 

 

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM