MVP框架(登入例子實現)
阿新 • • 發佈:2018-12-12
1.首先開啟許可權
<uses-permission android:name="android.permission.INTERNET"/>
2.xml程式碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activity.MainActivity" android:orientation="vertical"> <EditText android:id="@+id/editText1" android:hint="賬號" android:textSize="18sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/editText2" android:hint="密碼" android:textSize="18sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/checkBox1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textSize="18sp" android:text="記住密碼" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textSize="18sp" android:text="自動登入" /> </LinearLayout> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登入" android:textSize="18sp"/> </LinearLayout>
3.V層(MainActivity)
package com.example.zhoukao.activity; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import com.example.zhoukao.R; import com.example.zhoukao.bean.User; import com.example.zhoukao.core.IBaseView; import com.example.zhoukao.presenter.MyPresenter; public class MainActivity extends AppCompatActivity implements IBaseView { private CheckBox checkBox1; private CheckBox checkBox2; private Button button1; private EditText editText1; private EditText editText2; private MyPresenter myPresenter; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkBox1 = findViewById(R.id.checkBox1); checkBox2 = findViewById(R.id.checkBox2); button1 = findViewById(R.id.button1); editText1 = findViewById(R.id.editText1); editText2 = findViewById(R.id.editText2); myPresenter = new MyPresenter(this); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = editText1.getText().toString(); String pwd = editText2.getText().toString(); myPresenter.login(name,pwd); } }); sp = getSharedPreferences("login",MODE_PRIVATE); if(sp.getBoolean("c1",false)){ editText1.setText(sp.getString("name","")); editText2.setText(sp.getString("pwd","")); checkBox1.setChecked(true); } if(sp.getBoolean("c2",false)){ startActivity(new Intent(MainActivity.this,TwoActivity.class)); finish(); } } @Override public void success(User user) { Toast.makeText(this, ""+user.getMsg(), Toast.LENGTH_SHORT).show(); startActivity(new Intent(MainActivity.this,TwoActivity.class)); SharedPreferences.Editor editor = sp.edit(); String name = editText1.getText().toString(); String pwd = editText2.getText().toString(); editor.putString("name",name); editor.putString("pwd",pwd); editor.putBoolean("c1",checkBox1.isChecked()); editor.putBoolean("c2",checkBox2.isChecked()); editor.commit(); } @Override public void fail(String info) { Toast.makeText(this, ""+info, Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); new MyPresenter(); } }
P層
package com.example.zhoukao.presenter; import android.os.Handler; import android.os.Message; import com.example.zhoukao.bean.User; import com.example.zhoukao.core.IBaseView; import com.example.zhoukao.model.MyModel; public class MyPresenter { IBaseView iBaseView; public MyPresenter(IBaseView iBaseView) { this.iBaseView = iBaseView; } public MyPresenter(){ this.iBaseView = null; } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); User user = (User) msg.obj; if(user.getCode().equals("0")){ iBaseView.success(user); }else{ iBaseView.fail(user.getMsg()); } } }; public void login(final String name, final String pwd) { new Thread(new Runnable() { @Override public void run() { User user = MyModel.getString(name,pwd); Message message = handler.obtainMessage(); message.obj = user; handler.sendMessage(message); } }).start(); } }
M層
package com.example.zhoukao.model;
import com.example.zhoukao.bean.User;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MyModel {
public static User getString(String name, String pwd) {
String urlString = "http://www.zhaoapi.cn/user/login?mobile=" + name + "&password=" + pwd;
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str1 = "";
String str2 = "";
while ((str1 = reader.readLine())!= null){
str2 += str1;
}
Gson gson = new Gson();
User user = gson.fromJson(str2, User.class);
return user;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
自定義介面
package com.example.zhoukao.core;
import com.example.zhoukao.bean.User;
public interface IBaseView {
void success(User user);
void fail(String info);
}