这是登录页面的xml界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.yang1128.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:textSize="25sp" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/> RelativeLayout> <EditText android:id="@+id/ed_tel" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="10dp" android:hint="请输入手机号"/> <EditText android:id="@+id/ed_pass" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="10dp" android:hint="请输入密码"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp"> <Button android:id="@+id/btn_Login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="登录"/> <Button android:id="@+id/btn_Zhuce" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="注册"/> LinearLayout> LinearLayout>
这是配合的登录页面的Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { /** * 请输入手机号 */ private EditText mEdTel; /** * 请输入密码 */ private EditText mEdPass; /** * 登录 */ private Button mBtnLogin; /** * 注册 */ private Button mBtnZhuce; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mEdTel = (EditText) findViewById(R.id.ed_tel); mEdPass = (EditText) findViewById(R.id.ed_pass); mBtnLogin = (Button) findViewById(R.id.btn_Login); mBtnLogin.setOnClickListener(this); mBtnZhuce = (Button) findViewById(R.id.btn_Zhuce); mBtnZhuce.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.btn_Login: String mobile = mEdTel.getText().toString(); String password = mEdPass.getText().toString(); //判断输入的内容是否为phone boolean b = isPhoneNumber(mobile); if (mobile.isEmpty() || password.isEmpty()) { Toast.makeText(MainActivity.this, "用户名/密码不能为空", Toast.LENGTH_SHORT).show(); } else if (!b) { Toast.makeText(MainActivity.this, "手机号不合法", Toast.LENGTH_SHORT).show(); } else if (password.length() < 6) { Toast.makeText(MainActivity.this, "密码不能少于六位数", Toast.LENGTH_SHORT).show(); } else { login(mobile, password); } break; case R.id.btn_Zhuce: Intent intent_zhuce = new Intent(this, ZhuceActivity.class); startActivity(intent_zhuce); break; } } private boolean isPhoneNumber(String phoneStr) { //定义电话格式的正则表达式 String regex = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; //设定查看模式 Pattern p = Pattern.compile(regex); //判断Str是否匹配,返回匹配结果 Matcher m = p.matcher(phoneStr); return m.find(); } private void login(String mobile, String password) { RequestParams params = new RequestParams("http://120.27.23.105/user/login"); params.addQueryStringParameter("mobile", mobile); params.addQueryStringParameter("password", password); x.http().get(params, new Callback.CacheCallback() { @Override public void onSuccess(String result) { //成功 Gson gson = new Gson(); LoginBean loginBean = gson.fromJson(result, LoginBean.class); Toast.makeText(MainActivity.this, loginBean.getMsg(), Toast.LENGTH_SHORT).show(); if (loginBean.getCode().equals("0")) { Intent intent = new Intent(MainActivity.this, Main3Activity.class); startActivity(intent); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } @Override public boolean onCache(String result) { return false; } }); } }
这是注册页面的xml界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.yang1128.ZhuceActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:textSize="25sp" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/> RelativeLayout> <EditText android:id="@+id/zhu_tel" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="10dp" android:hint="请输入手机号"/> <EditText android:id="@+id/zhu_pass" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="10dp" android:hint="请输入密码"/> <Button android:id="@+id/Zhuce" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="立即注册"/> LinearLayout>
这是注册页面的Activity
public class ZhuceActivity extends AppCompatActivity implements View.OnClickListener { /** * 请输入手机号 */ private EditText mZhuTel; /** * 请输入密码 */ private EditText mZhuPass; /** * 立即注册 */ private Button mZhuce; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zhuce); initView(); } private void initView() { mZhuTel = (EditText) findViewById(R.id.zhu_tel); mZhuPass = (EditText) findViewById(R.id.zhu_pass); mZhuce = (Button) findViewById(R.id.Zhuce); mZhuce.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.Zhuce: String mobile = mZhuTel.getText().toString().trim(); String password = mZhuPass.getText().toString().trim(); //判断输入的内容是否为phone boolean b = isPhoneNumber(mobile); if (mobile.isEmpty() || password.isEmpty()) { Toast.makeText(this, "用户名/密码不能为空", Toast.LENGTH_SHORT).show(); } else if (!b) { Toast.makeText(this, "手机号不合法", Toast.LENGTH_SHORT).show(); } else if (password.length() < 6) { Toast.makeText(this, "密码不能少于六位数", Toast.LENGTH_SHORT).show(); } else { register(mobile, password); } break; } } private boolean isPhoneNumber(String phoneStr) { //定义电话格式的正则表达式 String regex = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; //设定查看模式 Pattern p = Pattern.compile(regex); //判断Str是否匹配,返回匹配结果 Matcher m = p.matcher(phoneStr); return m.find(); } private void register(String mobile,String password){ RequestParams params = new RequestParams("http://120.27.23.105/user/reg"); params.addQueryStringParameter("mobile",mobile); params.addQueryStringParameter("password",password); x.http().get(params, new Callback.CacheCallback() { @Override public void onSuccess(String result) { //成功 Gson gson = new Gson(); RegistBean registBean = gson.fromJson(result, RegistBean.class); //如果注册成功就返回登录页面 Toast.makeText(ZhuceActivity.this,registBean.getMsg(), Toast.LENGTH_SHORT).show(); if (registBean.getCode().equals("0")){ finish(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } @Override public boolean onCache(String result) { return false; } }); } }
这是商品列表界面,但是这个界面没有什么功能,只有布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.yang1128.Main3Activity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商品列表" android:textSize="25sp" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/> RelativeLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是商品列表" android:layout_gravity="center" android:textSize="30sp" android:layout_marginTop="50dp"/> LinearLayout>
接下来写登录和注册的bean数据
public class LoginBean { private String msg; private String code; public String getCode() { return code; } public String getMsg() { return msg; } public void setCode(String code) { this.code = code; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return "LoginBean{" + "code='" + code + '\'' + ", msg='" + msg + '\'' + '}'; } }
public class RegistBean { private String msg; private String code; private String 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 String getData() { return data; } public void setData(String data) { this.data = data; } }
这是比较重要的一个类
public class RegistApplication extends Application{ public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(BuildConfig.DEBUG); } }
记得在清单文件里面配置这个类。
这是XUtils的依赖。
compile 'org.xutils:xutils:3.5.0'
这样用XUtils 框架实现的登录和注册页面就完成了。