1、MVC模型简介
MVC的全名是Model View Controller,即模型(model)- 视图(view)- 控制器(controller)
Controller:Activity、Fragment
View:layout、View控件
Model:数据处理(网络请求,SQL等)
示例:
1、Account.java
package com.example.moduledemo.bean;
/**
* author : huangyi
* date : 2020/2/17
* desc :
*/
public class Account {
private String name;//账号名称
private int level;//账号等级
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
}
2、MCallback.java
package com.example.moduledemo.callback;
import com.example.moduledemo.bean.Account;
/**
* author : huangyi
* date : 2020/2/17
* desc :
*/
public interface MCallback {
void onSuccess(Account account);
void onFailed();
}
3、MVCModel.java
package com.example.moduledemo.mvc;
import com.example.moduledemo.bean.Account;
import com.example.moduledemo.callback.MCallback;
import java.util.Random;
/**
* author : huangyi
* date : 2020/2/17
* desc :
*/
public class MVCModel {
//获取账号数据
public void getAccountData(String accountName, MCallback callback) {
Random random = new Random();
boolean isSuccess = random.nextBoolean();
if (isSuccess) {
Account account = new Account();
account.setName(accountName);
account.setLevel(100);
callback.onSuccess(account);
} else {
callback.onFailed();
}
}
}
4、activity_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:hint="请输入要查询的账号" />
<Button
android:id="@+id/btn_getAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="80dp"
android:text="获取账号信息" />
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="80dp"
android:hint="账号信息未获取" />
</LinearLayout>
5、MVCActivity.java
package com.example.moduledemo.mvc;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.moduledemo.R;
import com.example.moduledemo.bean.Account;
import com.example.moduledemo.callback.MCallback;
/**
* author : huangyi
* date : 2020/2/17
* desc :
*/
public class MVCActivity extends AppCompatActivity {
private TextView mTvResult;
private EditText mEtAccount;
private Button mBtnGetAccount;
private MVCModel mvcModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
initView();
mvcModel = new MVCModel();
setListener();
}
private void initView() {
mTvResult = findViewById(R.id.tv_result);
mEtAccount = findViewById(R.id.et_account);
mBtnGetAccount = findViewById(R.id.btn_getAccount);
}
//获取用户输入信息
private String getUserInput() {
return mEtAccount.getText().toString();
}
//展示获取数据成功的界面
private void showSuccessPage(Account account) {
mTvResult.setText("用户账号:" + account.getName() + " | " + " 用户等级:" + account.getLevel());
}
//展示获取数据失败的界面
private void showFailedPage() {
mTvResult.setText("获取数据失败");
}
private void setListener() {
mBtnGetAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mvcModel.getAccountData(getUserInput(), new MCallback() {
@Override
public void onSuccess(Account account) {
showSuccessPage(account);
}
@Override
public void onFailed() {
showFailedPage();
}
});
}
});
}
}