1. 程式人生 > >XUtils結合使用的登入和註冊

XUtils結合使用的登入和註冊

   這是登入頁面的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<String>() {
            @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<String>() {
            @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 框架實現的登入和註冊頁面就完成了。