com.ymt.demo1.main.sign.SignUpFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.ymt.demo1.main.sign.SignUpFragment.java

Source

package com.ymt.demo1.main.sign;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.ymt.demo1.R;
import com.ymt.demo1.customViews.MyCheckView;
import com.ymt.demo1.utils.AppContext;
import com.ymt.demo1.utils.BaseURLUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.tablemanager.Connector;

/**
 * Created by Moses on 2015
 */
public class SignUpFragment extends Fragment {
    public static final String FLAG = "SignUpFragment";
    public static final String NORMAL_USER = "app";
    //    public static final String EXPORT_USER = "002";
    //    public static final String MEMBER_USER = "003";
    //    public static final String MANAGER_USER = "004";
    private RequestQueue queue;
    private MyCheckView myCheckView;
    //&loginname=moses&pwd=123&phone=333&t=001(t001002003004)
    private SharedPreferences sharedPreferences;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_sign_up, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreferences = getActivity().getSharedPreferences(SignInFragment.SIGN_IN_SETTING,
                Context.MODE_PRIVATE);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Connector.getDatabase();
        queue = Volley.newRequestQueue(getActivity());
    }

    /**
     * ???
     * ?????????
     */
    protected void initView(View view) {
        /*
        
         */
        //?
        final EditText phoneTxt = (EditText) view.findViewById(R.id.input_phone_num);
        final EditText accountTxt = (EditText) view.findViewById(R.id.input_account);
        final EditText pswTxt = (EditText) view.findViewById(R.id.input_psw);
        final EditText rePswTxt = (EditText) view.findViewById(R.id.re_input_psw);
        final TextView licenseTxt = (TextView) view.findViewById(R.id.licence_txt);
        /*???? ??
         */
        final TextView getCheckNums = (TextView) view.findViewById(R.id.get_check_nums);
        final EditText inputCheckNums = (EditText) view.findViewById(R.id.input_check_nums);
        getCheckNums.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo ????

            }
        });

        /**
         *  todo spannableString 
         *   ?
         */
        SpannableString spannableString = new SpannableString(getString(R.string.license));
        ForegroundColorSpan span = new ForegroundColorSpan(getResources().getColor(R.color.material_blue_grey_800));
        spannableString.setSpan(span, 0, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        licenseTxt.setText(spannableString);
        //switch(??)
        myCheckView = (MyCheckView) view.findViewById(R.id.switch_liscense);
        //
        Button signUpBtn = (Button) view.findViewById(R.id.do_sign_btn);
        signUpBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //
                String phoneNum = phoneTxt.getText().toString();
                String account = accountTxt.getText().toString();
                String psw = pswTxt.getText().toString();
                String rePsw = rePswTxt.getText().toString();
                //??
                String checkNums = inputCheckNums.getText().toString();
                //todo if(){}else{} ??

                if (!TextUtils.isEmpty(phoneNum) && !TextUtils.isEmpty(account) && !TextUtils.isEmpty(psw)
                        && psw.equals(rePsw)) {
                    //?
                    if (myCheckView.isChecked()) {
                        queue.add(signUpRequest(account, psw, phoneNum, NORMAL_USER));
                    } else {
                        Toast.makeText(getActivity(), "?????",
                                Toast.LENGTH_SHORT).show();
                    }

                } else if (TextUtils.isEmpty(phoneNum)) {
                    //???
                    Toast.makeText(getActivity(), "???", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(account)) {
                    //???
                    Toast.makeText(getActivity(), "???", Toast.LENGTH_SHORT).show();
                } else {
                    //???
                    Toast.makeText(getActivity(), "???", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });

        LinearLayout licence = (LinearLayout) view.findViewById(R.id.liscense_layout);
        licence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myCheckView.callOnClick();
            }
        });
    }

    protected void jumpToSignIn(String account, String psw) {
        Toast.makeText(getActivity(), "??", Toast.LENGTH_SHORT).show();
        onDestroyView();
        //???sharedPreference
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(SignInFragment.REMEMBERED_NAME, account);
        editor.putString(SignInFragment.REMEMBERED_PSW, psw);
        editor.apply();

        SignInUpActivity activity = (SignInUpActivity) getActivity();
        activity.setSelect(0);
    }

    /**
     * ? 
     *
     * @param account   sign account
     * @param psw       : sign password
     * @param phone     : sign phone_num
     * @param user_type : sign type (normal_user,export_user,manager_user...)
     * @return : VOLLEY Request
     */
    protected StringRequest signUpRequest(final String account, final String psw, final String phone,
            final String user_type) {
        String loginBaseUrl = BaseURLUtil.doSignUp(account, psw, phone, user_type);
        //        Log.e("TAG", ">>>>>>>>>>>signUP url: " + loginBaseUrl);
        return new StringRequest(loginBaseUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.getJSONObject("datas").optBoolean("listData")) {
                        //sign successfully
                        jumpToSignIn(account, psw);
                    } else {
                        //sign unsuccessfully
                        Toast.makeText(getActivity(), jsonObject.getJSONObject("datas").opt("listData").toString(),
                                Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    AppContext.toastBadJson();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                AppContext.toastBadInternet();
            }
        });
    }

}