这篇文章主要介绍了Android中如何利用MVP实现登录注册功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android中如何利用MVP实现登录注册功能文章都会有所收获,下面我们一起来看看吧。
成都网站建设哪家好,找成都创新互联!专注于网页设计、成都网站建设、微信开发、小程序开发、集团成都企业网站建设等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:水泥搅拌车等众多领域,积累了大量丰富的经验,同时也获得了客户的一致称誉!model包:
import com.bwei.mvps.bean.UserBean; public interface IUserModel { void setFirstName(String firstName); void setLastName(String lastName); String getFirstName(); String getLastName(); //根据id获取对象 UserBean load(int id); }
import android.util.Log; import com.bwei.mvps.bean.UserBean; public class UserModel implements IUserModel { @Override public void setFirstName(String firstName) { Log.i("xxx", firstName); } @Override public void setLastName(String lastName) { Log.i("xxx", lastName); } @Override public String getFirstName() { return null; } @Override public String getLastName() { return null; } @Override public UserBean load(int id) { //查询数据库或联网获取数据 Log.i("fff", id + ""); return new UserBean("张", "三"); } }
View包
public interface UserView { void setFirstName(String firstName); void setLastName(String lastName); int getId(); String getFirstName(); String getLastName(); }
presenter包:
import android.util.Log; import com.bwei.mvps.MainActivity; import com.bwei.mvps.bean.UserBean; import com.bwei.mvps.model.IUserModel; import com.bwei.mvps.model.UserModel; import com.bwei.mvps.view.UserView; public class UserPresenter { private UserView userview; private final IUserModel iUserModel; public UserPresenter(UserView userview) { this.userview = userview; iUserModel = new UserModel(); } //保存数据 public void saveUser(int id, String firstName, String lastName) { UserBean userBean = iUserModel.load(id); Log.i("sss", "id:" + id + ",firstName:" + firstName + ",lastName:" + lastName); } //查询数据 public void find(int id) { UserBean userBean = iUserModel.load(id); String firstName = userBean.getFirstName(); String lastName = userBean.getLastName(); userview.setFirstName(firstName); userview.setLastName(lastName); Log.i("aaa", "id:" + id + ",firstName:" + firstName + ",lastName:" + lastName); } }
XML
Mactivity
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.bwei.mvps.presenter.UserPresenter; import com.bwei.mvps.view.UserView; public class MainActivity extends AppCompatActivity implements View.OnClickListener, UserView { private EditText et_id; private EditText et_first_name; private EditText et_last_name; private Button bt_login; private Button bt_register; private UserPresenter userPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 et_id = (EditText) findViewById(R.id.et_id); et_first_name = (EditText) findViewById(R.id.et_first_name); et_last_name = (EditText) findViewById(R.id.et_last_name); bt_login = (Button) findViewById(R.id.bt_login); bt_register = (Button) findViewById(R.id.bt_register); bt_login.setOnClickListener(this); bt_register.setOnClickListener(this); //声明UserPresenter userPresenter = new UserPresenter(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_register://保存数据 userPresenter.saveUser(getId(), getFirstName(), getLastName()); break; case R.id.bt_login: userPresenter.find(getId()); break; } } @Override public void setFirstName(String firstName) { et_first_name.setText(firstName); } @Override public void setLastName(String lastName) { et_last_name.setText(lastName); } @Override public int getId() { return new Integer(et_id.getText().toString()); } @Override public String getFirstName() { return et_first_name.getText().toString(); } @Override public String getLastName() { return et_last_name.getText().toString(); } }
关于“Android中如何利用MVP实现登录注册功能”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Android中如何利用MVP实现登录注册功能”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。