Android MVP模式简单易懂的介绍方式 (三)
讲完M和P,接下来就要讲V了。View也很好理解,就是把UI需要做的事情,通过接口暴露出来。那么UI要做什么事情,无非就是对UI控件的操作(这里是清空输入)、还有登陆成功或者失败的操作嘛。因此,我们可以这样写。
public interface ILoginView { void onClearText(); void onLoginReuslt(Boolean res,int code); }
一样,有了接口,当然需要一个类来实现他。那么谁来实现这个接口呢?当然是我们的Activity。Activity实现了ILoginView接口后,由于逻辑的调用我们都在钱面写完了。因此,只需要写好UI处理即可。为了和ILoginPresenter 通讯,所以Activity需要一个ILoginPresenter ,然后,由ILoginPresenter 去和ILoginView进行通讯,从而实现UI与业务的分离
public class LoginActivity extends AppCompatActivity implements ILoginView,View.OnClickListener{ private EditText et_name; private EditText et_password; ILoginPresenter iLoginPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.setTitle("登陆"); findViewById(R.id.bt_login).setOnClickListener(this); findViewById(R.id.bt_clear).setOnClickListener(this); et_name = findViewById(R.id.et_username); et_password = findViewById(R.id.et_password); iLoginPresenter = new ILoginPresenterCompl(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_clear: iLoginPresenter.clear(); break; case R.id.bt_login: iLoginPresenter.doLogin(et_name.getText().toString(),et_password.getText().toString()); break; default: } } @Override public void onClearText() { et_name.setText(""); et_password.setText(""); } @Override public void onLoginReuslt(Boolean res, int code) { switch (code){ case 0: break; case 1: Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show(); break; case -1: Toast.makeText(this,"用户名错误",Toast.LENGTH_SHORT).show(); break; case -2: Toast.makeText(this,"密码错误",Toast.LENGTH_SHORT).show(); break; default: } } }
为了方便大家,顺便把布局文件也贴上,那么到这里,我们整一个MVP就完成了。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.himarking.myapplication.MainActivity"> <LinearLayout android:orientation="vertical" android:layout_marginTop="100dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_margin="10dp" android:layout_marginTop="200dp" android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:layout_margin="10dp" android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_marginTop="200dp" android:layout_margin="10dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/bt_login" android:text="登陆" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/bt_clear" android:text="清空" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现