您好,登錄后才能下訂單哦!
登錄代碼MVP架構的具體實現,分享給大家。
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener, Mvp.fff { private EditText mUsername; private EditText mPassword; private Button login; private ProgressDialog mProgressDialog; private Mvp mMvp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); mMvp = new Mvp(this); } private void initView() { mUsername = (EditText) findViewById(R.id.username); mPassword = (EditText) findViewById(R.id.password); login = (Button) findViewById(R.id.login); login.setOnClickListener(this); //動態的創建一個進度條 mProgressDialog = new ProgressDialog(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.login: String user = mUsername.getText().toString(); String pass = mPassword.getText().toString(); final User users = new User(); users.username=user; users.password=pass; Boolean empty = mMvp.isEmpty(users); if (empty){ mProgressDialog.show(); mMvp.isLogin(users); }else { Toast.makeText(MainActivity.this, "輸入不能為空", Toast.LENGTH_SHORT).show(); return; } break; } } @Override public void success() { runOnUiThread(new Runnable() { @Override public void run() { mProgressDialog.dismiss(); Toast.makeText(MainActivity.this, "歡迎回來,思密達", Toast.LENGTH_SHORT).show(); } }); } @Override public void doil() { runOnUiThread(new Runnable() { @Override public void run() { mProgressDialog.dismiss(); Toast.makeText(MainActivity.this, "登錄失敗,賬號密碼有問題", Toast.LENGTH_SHORT).show(); } }); } @Override protected void onDestroy() { super.onDestroy(); mMvp=null; } }
UserLogin.java
public class UserLogin { //MVC Controller層 public boolean login(User users){ SystemClock.sleep(1000); if ("frq".equals(users.username)&&"frq".equals(users.password)){ return true; }else { return false; } } }
Mvp.java
public class Mvp { //MVP Precenter層 public Mvp(fff fff) { mFff = fff; } public Boolean isEmpty(User users){ if (TextUtils.isEmpty(users.username) || TextUtils.isEmpty(users.password)){ return false; }else { return true; } } public void isLogin(final User users){ new Thread(){ @Override public void run() { UserLogin login = new UserLogin(); boolean b = login.login(users); if (b){ mFff.success(); }else { mFff.doil(); } } }.start(); } public interface fff{ void success(); void doil(); } private fff mFff; } =========================================== public class Precenter { public Precenter(fff fff) { mFff = fff; } public void As(String urlString){ new AsyncTask<String, Void, ArrayList<FirstBean.ResultBean>>() { @Override protected ArrayList<FirstBean.ResultBean> doInBackground(String... strings) { String s = new HttpUrl().toString(strings[0]); //調用外部類 FirstBean firstBean = new Gson().fromJson(s, FirstBean.class); //gson解析 if (firstBean!=null){ return (ArrayList<FirstBean.ResultBean>) firstBean.getResult(); } return null; } @Override protected void onPostExecute(ArrayList<FirstBean.ResultBean> resultBeans) { super.onPostExecute(resultBeans); if (resultBeans!=null){ mFff.success(resultBeans); }else { mFff.bai(); } } }.execute(urlString); } public interface fff{ void success(ArrayList<FirstBean.ResultBean> resultBeans); void bai(); }; private fff mFff; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。