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

來源:互聯網
上載者:User

標籤:

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

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

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

 

    

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.