package com.bawei.dianshang;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.bawei.dianshang.presenter.LoginPresenter;
import com.bawei.dianshang.view.LoginIView;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener,LoginIView {
private EditText ed_phone;
private EditText ed_pwd;
private Button login;
private Button zhuce;
private String ephone;
private String epwd;
private LoginPresenter loginPresenter;
private Handler myhandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//控件
ed_phone = findViewById(R.id.ed_phone);
ed_pwd = findViewById(R.id.ed_pwd);
login = findViewById(R.id.login);
zhuce = findViewById(R.id.zhuce);
login.setOnClickListener(this);
zhuce.setOnClickListener(this);
loginPresenter = new LoginPresenter();
loginPresenter.attahcView(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.login:
ephone = ed_phone.getText().toString();
epwd = ed_pwd.getText().toString();
if (ephone.length()<11){
Toast.makeText(this, "手機號格式不正確", Toast.LENGTH_SHORT).show();
return;
}else{
if (epwd.length()<3){
Toast.makeText(this, "密碼格式不正確", Toast.LENGTH_SHORT).show();
return;
}else {
loginPresenter.requestData(ephone,epwd);
}
}
break;
case R.id.zhuce:
Intent intent = new Intent(LoginActivity.this,ZhuceActivity.class);
startActivity(intent);
break;
}
}
@Override
public void showData(String yyy) {
try {
JSONObject jsonObject = new JSONObject(yyy);
final String message = jsonObject.getString("message");
myhandler.post(new Runnable() {
@Override
public void run() {
if (message.equals("登錄成功")){
Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this,ShouActivity.class);
startActivity(intent);
}else {
Toast.makeText(LoginActivity.this, "登錄失敗", Toast.LENGTH_SHORT).show();
}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
loginPresenter.detachView(this);
}
}