0
我是Android新手,嘗試學習MVP模式。我在下面寫了示例教程,它從EditText獲取用戶名和密碼,並查看它是否匹配「Don」和「Bradman」。如果是,它會相應地顯示Toast消息。我的問題是可以將比較邏輯保存在MVP模式下的活動中
「是否可以從活動中的編輯文本中獲取值並進行比較?我不這麼認爲,因爲那時業務邏輯出現在活動中,這不是正確的做法。我得到的那些編輯文本值演示類和匹配那裏,將消息發送到活動顯示吐司下面是我的代碼,如果有人能幫助「。
MainInterface.java
public interface MainInterface {
//Declare all Activity related methods here
interface View{
void checkLoginSuccessful();
}
//Declare all Model related methods here
interface Model{
String getUsername();
String setUsername(String username);
String getPassword();
String setPassword(String password);
}
//Declare all Presenter related methods here -- All Business Logic comes here
interface Presenter {
void onClickLogin();
}
}
PresenterClass.java
public class PresenterClass implements MainInterface.Presenter{
//Now since presenter is going to interact with both classes Model and View so let's invoke both "DataModel" class and "MainActivity" class
private MainInterface.View mview;
public PresenterClass(MainInterface.View view) {
this.mview = view;
}
@Override
public void onClickLogin() {
mview.checkLoginSuccessful();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements MainInterface.View {
private EditText username;
private EditText password;
private Button loginbutton;
PresenterClass presenter;
//DataModel data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenter = new PresenterClass(this);
username = (EditText) findViewById(R.id.editusername);
password = (EditText) findViewById(R.id.editpassword);
loginbutton = (Button) findViewById(R.id.loginbutton);
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
presenter.onClickLogin();
}
});
}
@Override
public void checkLoginSuccessful() {
if (username.getText().toString().equals("Don") && password.getText().toString().equals("Bradman")){
Toast.makeText(MainActivity.this, "You have successfully logged in", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Wrong Username or Password", Toast.LENGTH_SHORT).show();
}
}
}