Android入门(五):程序架构——MVC设计模式在Android中的应用

mac2022-06-30  24

  刚刚接触编程的的人,可能会这样认为:只要代码写完了能够跑起来就算完工了。如果只是写一个小程序,“能够跑起来”这样的标准也就可以了,但是如果你是在公司进行程序的开发,那么仅仅让程序成功的跑起来是不行的,事情远没有你想的这么简单。一个商业项目的代码少则数万行,多则上百万甚至更多,这种商业项目不可能仅仅靠一个人完成,要想高效高质量的完成开发工作,就需要一个专业的开发团队了。在团队中,有人负责项目的架构设计,有些人负责程序代码的编写….要想像这样做到项目开发的分工就必须在程序的结构上做适当的安排。

  举个例子,大多数商业化软件都有不同的语言版本,这些不同语言版本的软件在功能上是完全一样的,如果我们能够把软件上的文字与程序分离开来,这样就能够很方便的发布不同语言的版本了。

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

 

    

转载于:https://www.cnblogs.com/peterpan-/p/5769076.html

相关资源:Android开发中的MVC设计模式浅析
最新回复(0)