android中如何实现mvc

 我来答
pekdou
推荐于2017-09-09 · 超过12用户采纳过TA的回答
知道答主
回答量:27
采纳率:0%
帮助的人:31万
展开全部
用MVC进行重构,例:
Step1:抽取所有界面元件的声明和定义,整合到单独一个函数findViews()中;
// 声明
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;

// 定义
private void findViews() {
button_calc = (Button) findViewById(R.id.submit);
field_height = (EditText) findViewById(R.id.height);
field_weight = (EditText) findViewById(R.id.weight);
view_result = (TextView) findViewById(R.id.result);
view_suggest = (TextView) findViewById(R.id.suggest);
}
此部分即是MVC中的V:View视图。
Step2:抽取程序的逻辑(即界面元件的处理逻辑),整合到函数setListensers()中;
//Listen for button clicks
private void setListensers() {
button_calc.setOnClickListener(calcBMI);
}
此部分即是MVC中的C:Controller控制器。
接着,onCreate()就显得非常简洁、明了了:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

findViews();
setListensers();
}
Step3:修改按钮监听器calcBMI中相应的部分(主要是变量已经在视图部分定义了);
private OnClickListener calcBMI = new OnClickListener() {
@Override
public void onClick(View v) {
DecimalFormat nf = new DecimalFormat("0.00");

double height = Double.parseDouble(field_height.getText().toString()) / 100;
double weight = Double.parseDouble(field_weight.getText().toString());
double BMI = weight / (height * height);

// Present result
view_result.setText("Your BMI is " + nf.format(BMI));

// Give health advice
if (BMI > 25) {
view_suggest.setText(R.string.advice_heavy);
} else if (BMI < 20) {
view_suggest.setText(R.string.advice_light);
} else {
view_suggest.setText(R.string.advice_average);
}
}
};
Step4:修剪一下“细枝末节”;
1) calcBMI的修改:
private OnClickListener calcBMI = new OnClickListener() {
……
}
改成如下:
private Button.OnClickListener calcBMI = new Button.OnClickListener() {
……
}
gasolin的解释是:
同样是「calcBMI」 函式,在完整程式中,改将「calcBMI」 函式从原本的「OnClickListener」宣告成 「Button.OnClickListener」。这个改变有什麼差别呢?
阅读原本的程式码,在汇入(import)的部分可以看到,「OnClickListener」是来自於「android.view.View.OnClickListener」函式:
import android.view.View.OnClickListener;
改成 「Button.OnClickListener」後,「Button.OnClickListener」就变成来自於「android.widget.Button」中的「OnClickListener」函式,在查阅程式时,整个「Button」与「OnClickListener」之间的关系变得更清晰。
2) 字符串统一引用XML中的描述符:
//Present result
view_result.setText("Your BMI is " + nf.format(BMI));
改成如下:
//Present result
view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));

总之,此重构的目的无非是使程序的脉络更加清晰,即让人一眼望去,就能很容易地分辨出界面(View)应该写在哪里,程序逻辑(Controller)应该写在哪里,最终使维护和扩展代码变得更加容易!
重构前的代码Bmi.java:
package com.demo.android.bmi;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Bmi extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Listen for button clicks
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(calcBMI);
}

private OnClickListener calcBMI = new OnClickListener() {
@Override
public void onClick(View v) {
DecimalFormat nf = new DecimalFormat("0.00");
EditText fieldheight = (EditText) findViewById(R.id.height);
EditText fieldweight = (EditText) findViewById(R.id.weight);

double height = Double.parseDouble(fieldheight.getText().toString()) / 100;
double weight = Double.parseDouble(fieldweight.getText().toString());
double BMI = weight / (height * height);

TextView result = (TextView) findViewById(R.id.result);
result.setText("Your BMI is " + nf.format(BMI));

// Give health advice
TextView fieldsuggest = (TextView) findViewById(R.id.suggest);
if (BMI > 25) {
fieldsuggest.setText(R.string.advice_heavy);
} else if (BMI < 20) {
fieldsuggest.setText(R.string.advice_light);
} else {
fieldsuggest.setText(R.string.advice_average);
}
}
};
}

2) 重构后的代码Bmi.java:
package com.demo.android.bmi;

import java.text.DecimalFormat;

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 Bmi extends Activity {

private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

findViews();
setListensers();
}

// 定义
private void findViews() {
button_calc = (Button) findViewById(R.id.submit);
field_height = (EditText) findViewById(R.id.height);
field_weight = (EditText) findViewById(R.id.weight);
view_result = (TextView) findViewById(R.id.result);
view_suggest = (TextView) findViewById(R.id.suggest);
}

// Listen for button clicks
private void setListeners() {
calcbutton.setOnClickListener(calcBMI);
}

private Button.OnClickListener calcBMI = new Button.OnClickListener() {
public void onClick(View v) {
DecimalFormat nf = new DecimalFormat("0.0");
double height = Double.parseDouble(field_height.getText().toString()) / 100;
double weight = Double.parseDouble(field_weight.getText().toString());
double BMI = weight / (height * height);

// Present result
view_result.setText(getText(R.string.bmi_result) + nf.format(BMI));

// Give health advice
if (BMI > 25) {
view_suggest.setText(R.string.advice_heavy);
} else if (BMI < 20) {
view_suggest.setText(R.string.advice_light);
} else {
view_suggest.setText(R.string.advice_average);
}
}
};
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式