登录xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bwie.mango.LoginActivity">
<ImageView
android:id="@+id/login_img"
android:layout_marginLeft="180dp"
android:layout_marginTop="100dp"
android:src="@mipmap/tao"
android:layout_width="100dp"
android:layout_height="100dp" />
<EditText
android:id="@+id/username"
android:layout_marginTop="10dp"
android:layout_below="@id/login_img"
android:hint="手机号/会员名/邮箱"
android:layout_width="400dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/password"
android:layout_below="@id/username"
android:hint="请输入密码"
android:layout_marginTop="10dp"
android:layout_width="400dp"
android:layout_height="wrap_content" />
<TextView
android:text="新用户注册"
android:id="@+id/xin"
android:layout_marginTop="10dp"
android:layout_below="@id/password"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/login"
android:text="登录"
android:textColor="@color/colorse"
android:layout_below="@id/xin"
android:background="@color/colorba"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_width="400dp"
android:layout_height="wrap_content" />
</RelativeLayout>
注册xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_zhuce"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bwie.mango.ZhuceActivity">
<EditText
android:id="@+id/zc_user"
android:hint="请输入用户名"
android:layout_width="400dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/zc_pass"
android:layout_marginTop="10dp"
android:layout_below="@id/zc_user"
android:hint="请输入密码"
android:layout_width="400dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/queren"
android:layout_marginTop="10dp"
android:layout_below="@id/zc_pass"
android:hint="确认输入密码"
android:layout_width="400dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/email"
android:layout_marginTop="10dp"
android:layout_below="@id/queren"
android:hint="请填写邮箱"
android:layout_width="400dp"
android:layout_height="wrap_content" />
<Button
android:text="注册"
android:layout_marginTop="10dp"
android:id="@+id/zhuce"
android:layout_below="@id/email"
android:textColor="@color/colorse"
android:background="@color/colorba"
android:layout_width="400dp"
android:layout_height="wrap_content" />
</RelativeLayout>
user
public class User {
/**
* msg : 登录成功
* code : 0
* data : {"age":null,"createtime":"2017-10-14T00:00:00","gender":0,"icon":null,"mobile":"15727377159","money":0,"nickname":null,"password":"666666","uid":600,"username":"15727377159"}
*/
private String msg;
private String code;
//private DataBean data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
/* public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}*/
}
LoginActivity
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private TextView zhuce;
private Button login;
private EditText username,password;
// private HashMap<String, String> map;
private String url="http://120.27.23.105/user/login";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login= (Button) findViewById(R.id.login);
zhuce= (TextView) findViewById(R.id.xin);
username= (EditText) findViewById(R.id.username);
password= (EditText) findViewById(R.id.password);
/*zhuce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(LoginActivity.this,ZhuceActivity.class);
startActivity(intent);
}
});*/
/* Map map= new HashMap();
map.put("mobile","18622789651");
map.put("password","123456");*/
login.setOnClickListener(this);
zhuce.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.login:
String uname = username.getText().toString().trim();
String upass = password.getText().toString().trim();
HashMap map = new HashMap();
map.put("mobile",uname);
map.put("password",upass);
//String path=url + "?mobile=" + uname + "&password=" + upass;
if (uname != null || upass != null) {
OkHttp3Utils.getInstance().doPost(url, map, new GsonObjectCallback<User>() {
@Override
public void onUi(User user) {
String s = user.getMsg();
Toast.makeText(LoginActivity.this,s,Toast.LENGTH_SHORT).show();
if (s.equals("登录成功")){
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this,s,Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(LoginActivity.this,s,Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailed(Call call, IOException e) {
}
});
}else {
Toast.makeText(LoginActivity.this,"不能为空哦~",Toast.LENGTH_SHORT).show();
}
break;
case R.id.xin:
Intent intent2=new Intent(LoginActivity.this,ZhuceActivity.class);
startActivity(intent2);
break;
}
};
}
ZhuceActivity
private EditText user,pass,queren,email;
private Button zhuce;
private String url="http://120.27.23.105/user/reg";
private HashMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhuce);
user= (EditText) findViewById(R.id.zc_user);
pass= (EditText) findViewById(R.id.zc_pass);
queren= (EditText) findViewById(R.id.queren);
email= (EditText) findViewById(R.id.email);
zhuce= (Button) findViewById(R.id.zhuce);
zhuce.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String uname1 = user.getText().toString().trim();
String upass1 =pass.getText().toString().trim();
String queren1=queren.getText().toString().trim();
String email1=email.getText().toString().trim();
// String path=url+"?mobile="+uname+"&password="+upass;
map = new HashMap();
map.put("mobile",uname1);
map.put("password",upass1);
if (uname1!=null&&upass1!=null&&queren1!=null&&email1!=null)
{
OkHttp3Utils.getInstance().doPost(url, map, new GsonObjectCallback<User>() {
@Override
public void onUi(User user) {
String s=user.getMsg();
Toast.makeText(ZhuceActivity.this,s,Toast.LENGTH_SHORT).show();
if (s.equals("注册成功"))
{
Intent intent=new Intent(ZhuceActivity.this,LoginActivity.class);
startActivity(intent);
Toast.makeText(ZhuceActivity.this,s,Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(ZhuceActivity.this,s,Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailed(Call call, IOException e) {
}
});
}else{
Toast.makeText(ZhuceActivity.this,"必填项,不能为空",Toast.LENGTH_SHORT).show();
}
}
}