Android Introduction (V): Program Architecture--MVC design mode application in Android

Source: Internet
Author: User
Tags gettext

People who have just come into contact with programming may think this way: as long as the code is finished, you can run and get it finished. If you just write a small program, "can run up" such a standard is OK, but if you are in the company to develop the program, then just let the program successfully run up is not good, things are far from what you think it is so simple. A business project with less than tens of thousands of lines of code, more than millions or more, this kind of business project can not be done by one person alone, in order to achieve high-quality and efficient development work, you need a professional development team. In the team, someone is responsible for the project's architectural design, and some people are responsible for the programming of the code .... To imagine that the division of work on project development must be properly arranged in the structure of the program.

For example, most commercial software has a different language version, these different language versions of the software are functionally identical, if we can separate the text and programs on the software, so that it is convenient to publish different language versions.

The full name of MVC is the model View Controller, which is the abbreviation for the models-view-controller, a software design paradigm that organizes the code with a method of business logic, data, and interface display separation. Aggregating business logic into a single component does not require rewriting business logic while improving and personalizing the interface and user interaction.

Now, based on the previous code, we have made some changes to apply the MVC pattern to the program.

1. A new Setupviewcompoent () method is responsible for running the view-related program code, including getting the interface components in the interface layout file and setting up the event handlers in the interface components.

2. Put the string written in the program in the Strings.xml resource file, the string defined in the Strings.xml resource file is compiled and placed in resource class R, and then the program obtains the required string from the resource class R.

3. In the Main.xml interface layout file, we define the hint text inside the Strings.xml resource file, and then remove the string to use in the resource class R. The code is as follows:

    Strings.xml resource file:

<resources>  
<string name= "app_name" > Fitness Consulting </string>
<string name= "promptsex" > Sex:</string>
<string name= "Promptage" > Age:</string>
<string name= "Promptbtndosug" > Fitness Consulting </string>
<string name= "Sugresult" > Results:</string>
<string name= "Sugrun" > Running </string>
<string name= "Sugswim" > Swimming </string>
<string name= "sugsuggestion" > Health Consulting </string>
<string name= "Sexmale" > Men </string>
</resources>


 main.xml   file:

<?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>


To modify the program code:
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 () {
//Get interface components from resource class 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);
Listener of//button component events
Btndosug.setonclicklistener (Btndosugonclick);
}

private Button.onclicklistener Btndosugonclick = new Button.onclicklistener () {

Public void OnClick (view view) {
//Click the button to execute the code
String strsex = Edtsex.gettext (). toString ();
int iage = Integer.parseint (Edtage.gettext (). toString ());

String strsug = "Result:";
if (strsex.equals ("male"))
{
if (Iage <)
Strsug + = getString (r.string.sugrun);
else if (iage >)
Strsug + = getString (r.string.sugrun);
Else
Strsug + = getString (r.string.sugrun);
}
Else
{
if (Iage <)
Strsug + = getString (r.string.sugrun);
else if (iage >)
Strsug + = getString (r.string.sugswim);
Else
Strsug + = getString (r.string.sugswim);
}

Txtresult.settext (STRSUG);
}
};
}

    

Android Introduction (V): Program Architecture--MVC design mode application in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.