PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/yueai/src/main/java/com/yueai/activity/RegisterActivity.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 360 lines | 303 code | 38 blank | 19 comment | 22 complexity | e368eb543e1f769688c2ff8376257109 MD5 | raw file
  1. package com.yueai.activity;
  2. import android.app.Dialog;
  3. import android.graphics.Color;
  4. import android.os.Build;
  5. import android.os.Bundle;
  6. import android.text.TextUtils;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.Window;
  10. import android.view.WindowManager;
  11. import android.widget.Button;
  12. import android.widget.ImageView;
  13. import android.widget.TextView;
  14. import com.alibaba.fastjson.JSON;
  15. import com.library.dialog.OnSingleDialogClickListener;
  16. import com.library.dialog.OneWheelDialog;
  17. import com.library.utils.DialogUtil;
  18. import com.library.utils.ToastUtil;
  19. import com.library.utils.Util;
  20. import com.yueai.R;
  21. import com.yueai.base.BaseFragmentActivity;
  22. import com.yueai.bean.Register;
  23. import com.yueai.constant.IUrlConstant;
  24. import com.yueai.dialog.AgeSelectDialog;
  25. import com.yueai.event.FinishEvent;
  26. import com.yueai.utils.AppsflyerUtils;
  27. import com.yueai.utils.CommonRequestUtil;
  28. import com.yueai.utils.WheelView;
  29. import com.yueai.xml.PlatformInfoXml;
  30. import com.yueai.xml.UserInfoXml;
  31. import com.zhy.http.okhttp.OkHttpUtils;
  32. import com.zhy.http.okhttp.callback.Callback;
  33. import org.greenrobot.eventbus.EventBus;
  34. import org.greenrobot.eventbus.Subscribe;
  35. import java.util.ArrayList;
  36. import java.util.List;
  37. import butterknife.BindView;
  38. import butterknife.ButterKnife;
  39. import butterknife.OnClick;
  40. import okhttp3.Call;
  41. import okhttp3.Response;
  42. /**
  43. * 注册页面
  44. * Midified by zhangdroid on 2017/03/09.
  45. */
  46. public class RegisterActivity extends BaseFragmentActivity implements View.OnClickListener {
  47. @BindView(R.id.register_male)
  48. ImageView register_male;
  49. @BindView(R.id.register_female)
  50. ImageView register_female;
  51. @BindView(R.id.login)
  52. TextView login;
  53. @BindView(R.id.agreement)
  54. TextView mTvAgreement;
  55. @BindView(R.id.tv_sex)
  56. TextView tvSex;
  57. @BindView(R.id.tv_age)
  58. TextView tvAge;
  59. @BindView(R.id.register)
  60. TextView register;
  61. private List<String> mAgeList = new ArrayList<>();
  62. private String mSelectedText;
  63. private AgeSelectDialog.OnAgeSelectListener mOnAgeSelectListener;
  64. @Override
  65. protected int getLayoutResId() {
  66. return R.layout.activity_register;
  67. }
  68. @Override
  69. protected void initViewsAndVariables() {
  70. // 透明状态栏
  71. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {// 5.0及以上
  72. View decorView = getWindow().getDecorView();
  73. decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
  74. // 设置状态栏背景
  75. getWindow().setStatusBarColor(Color.TRANSPARENT);
  76. } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {// 4.4
  77. getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  78. }
  79. tvAge.setText("24");
  80. tvSex.setText("男");
  81. }
  82. @Override
  83. protected void addListeners() {
  84. register_male.setOnClickListener(this);
  85. register_female.setOnClickListener(this);
  86. login.setOnClickListener(this);
  87. mTvAgreement.setOnClickListener(this);
  88. }
  89. public void onClick(View view) {
  90. switch (view.getId()) {
  91. case R.id.register_male:
  92. DialogUtil.showAgeSelectDialog(getSupportFragmentManager(), getString(R.string.select_age), getString(R.string.register), getString(R.string.cancel), false, new OneWheelDialog.OnOneWheelDialogClickListener() {
  93. @Override
  94. public void onPositiveClick(View view, String selectedText) {
  95. if (selectedText != null) {
  96. regist("0", selectedText);
  97. }
  98. }
  99. @Override
  100. public void onNegativeClick(View view) {
  101. }
  102. });
  103. break;
  104. case R.id.register_female:
  105. DialogUtil.showAgeSelectDialog(getSupportFragmentManager(), getString(R.string.select_age), getString(R.string.register), getString(R.string.cancel), false, new OneWheelDialog.OnOneWheelDialogClickListener() {
  106. @Override
  107. public void onPositiveClick(View view, String selectedText) {
  108. if (selectedText != null) {
  109. regist("1", selectedText);
  110. }
  111. }
  112. @Override
  113. public void onNegativeClick(View view) {
  114. }
  115. });
  116. break;
  117. case R.id.login:
  118. Util.gotoActivity(RegisterActivity.this, LoginActivity.class, false);
  119. break;
  120. case R.id.agreement:
  121. DialogUtil.showXiYiSingleBtnDialog(getSupportFragmentManager(), getString(R.string.register_xieyi), getString(R.string.register_xieyi_content), getString(R.string.sure), true, new OnSingleDialogClickListener() {
  122. @Override
  123. public void onPositiveClick(View view) {
  124. }
  125. });
  126. break;
  127. }
  128. }
  129. public void regist(String gender, String age) {
  130. showLoading();
  131. Callback<Register> callback = new Callback<Register>() {
  132. @Override
  133. public Register parseNetworkResponse(Response response, int id) throws Exception {
  134. String resultJson = response.body().string();
  135. if (!TextUtils.isEmpty(resultJson)) {
  136. return JSON.parseObject(resultJson, Register.class);
  137. }
  138. return null;
  139. }
  140. @Override
  141. public void onError(Call call, Exception e, int id) {
  142. dismissLoading();
  143. ToastUtil.showShortToast(RegisterActivity.this, getString(R.string.register_fail));
  144. }
  145. @Override
  146. public void onResponse(Register response, int id) {
  147. if (response != null) {
  148. String isSucceed = response.getIsSucceed();
  149. if (!TextUtils.isEmpty(response.getMsg())) {
  150. ToastUtil.showShortToast(RegisterActivity.this, response.getMsg());
  151. }
  152. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  153. UserInfoXml.setUserInfo(response.getUserPandora());
  154. PlatformInfoXml.setToken(response.getToken());
  155. // 设置页眉和互推开关
  156. UserInfoXml.setIsShowHeadMsg(response.getIsShowHeadNotice());
  157. UserInfoXml.setIsShowHeadMsgNew(response.getIsShowHeadNotice2());
  158. UserInfoXml.setIsShowRecommendUser(response.getIsShowCommendUser());
  159. // appsflyer注册
  160. AppsflyerUtils.register();
  161. // 初始化数据字典
  162. CommonRequestUtil.initParamsDict(new CommonRequestUtil.OnCommonListener() {
  163. @Override
  164. public void onSuccess() {
  165. gotoQA();
  166. }
  167. @Override
  168. public void onFail() {
  169. gotoQA();
  170. }
  171. });
  172. } else {
  173. dismissLoading();
  174. ToastUtil.showShortToast(RegisterActivity.this, getString(R.string.register_fail));
  175. }
  176. } else {
  177. dismissLoading();
  178. ToastUtil.showShortToast(RegisterActivity.this, getString(R.string.register_fail));
  179. }
  180. }
  181. };
  182. OkHttpUtils.post()
  183. .url(IUrlConstant.URL_REGISTER)
  184. .addParams("gender", gender)
  185. .addParams("pushToken", PlatformInfoXml.getPushToken())
  186. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  187. .addParams("age", age)
  188. .build()
  189. .execute(callback);
  190. }
  191. private void gotoQA() {
  192. // 发送事件,finish其它Activity
  193. EventBus.getDefault().post(new FinishEvent());
  194. dismissLoading();
  195. Util.gotoActivity(RegisterActivity.this, AnswerQuestionActivity.class, true);
  196. }
  197. @Override
  198. protected void doRegister() {
  199. super.doRegister();
  200. EventBus.getDefault().register(this);
  201. }
  202. @Override
  203. protected void unregister() {
  204. super.unregister();
  205. EventBus.getDefault().unregister(this);
  206. }
  207. @Subscribe
  208. public void onEvent(FinishEvent event) {
  209. finish();
  210. }
  211. @Override
  212. protected void onCreate(Bundle savedInstanceState) {
  213. super.onCreate(savedInstanceState);
  214. // TODO: add setContentView(...) invocation
  215. ButterKnife.bind(this);
  216. }
  217. @OnClick({R.id.tv_sex, R.id.tv_age, R.id.register})
  218. public void onViewClicked(View view) {
  219. switch (view.getId()) {
  220. case R.id.tv_sex:
  221. showSexDialog();
  222. break;
  223. case R.id.tv_age:
  224. showAgeDialog();
  225. break;
  226. case R.id.register:
  227. String sex= (String) tvSex.getText();
  228. String age=(String) tvAge.getText();
  229. if(!(sex.length()>2 || age.length()>2)){
  230. if(sex.equals("女")){
  231. regist("1",age);
  232. }else{
  233. regist("0",age);
  234. }
  235. }else{
  236. ToastUtil.showShortToast(this,"请选择性别或者年龄");
  237. }
  238. break;
  239. }
  240. }
  241. private void showAgeDialog() {
  242. View view = getLayoutInflater().inflate(R.layout.dialog_select_age,
  243. null);
  244. final Dialog dialog = new Dialog(this, R.style.transparentFrameWindowStyle);
  245. dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
  246. ViewGroup.LayoutParams.WRAP_CONTENT));
  247. Window window = dialog.getWindow();
  248. final WheelView wheelView = (WheelView) view.findViewById(R.id.dialog_age_wheelview);
  249. for (int i = 18; i < 66; i++) {
  250. mAgeList.add(String.valueOf(i));
  251. }
  252. wheelView.setData(mAgeList);
  253. if (!TextUtils.isEmpty(tvAge.getText().toString())) {
  254. wheelView.setDefaultIndex(mAgeList.indexOf(tvAge.getText().toString()));
  255. }
  256. Button btnSure = (Button) view.findViewById(R.id.dialog_age_sure);
  257. btnSure.setOnClickListener(new View.OnClickListener() {
  258. @Override
  259. public void onClick(View v) {
  260. tvAge.setText(wheelView.getSelectedText());
  261. dialog.dismiss();
  262. }
  263. });
  264. // 设置显示动画
  265. window.setWindowAnimations(R.style.main_menu_animstyle);
  266. WindowManager.LayoutParams wl = window.getAttributes();
  267. wl.x = 0;
  268. wl.y = getWindowManager().getDefaultDisplay().getHeight();
  269. // 以下这两句是为了保证按钮可以水平满屏
  270. wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
  271. wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
  272. // 设置显示位置
  273. dialog.onWindowAttributesChanged(wl);
  274. // 设置点击外围解散
  275. dialog.setCanceledOnTouchOutside(true);
  276. dialog.show();
  277. }
  278. private void showSexDialog() {
  279. View view = getLayoutInflater().inflate(R.layout.sex_dialog,
  280. null);
  281. TextView tvMan = (TextView) view.findViewById(R.id.tv_man);
  282. TextView tvWoman = (TextView) view.findViewById(R.id.tv_woman);
  283. Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
  284. final Dialog dialog = new Dialog(this, R.style.transparentFrameWindowStyle);
  285. dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
  286. ViewGroup.LayoutParams.WRAP_CONTENT));
  287. Window window = dialog.getWindow();
  288. // 设置显示动画
  289. window.setWindowAnimations(R.style.main_menu_animstyle);
  290. WindowManager.LayoutParams wl = window.getAttributes();
  291. wl.x = 0;
  292. wl.y = getWindowManager().getDefaultDisplay().getHeight();
  293. // 以下这两句是为了保证按钮可以水平满屏
  294. wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
  295. wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
  296. // 设置显示位置
  297. dialog.onWindowAttributesChanged(wl);
  298. // 设置点击外围解散
  299. dialog.setCanceledOnTouchOutside(true);
  300. dialog.show();
  301. tvMan.setOnClickListener(new View.OnClickListener() {
  302. @Override
  303. public void onClick(View view) {
  304. tvSex.setText("男");
  305. dialog.dismiss();
  306. }
  307. });
  308. tvWoman.setOnClickListener(new View.OnClickListener() {
  309. @Override
  310. public void onClick(View view) {
  311. tvSex.setText("女");
  312. dialog.dismiss();
  313. }
  314. });
  315. btnCancel.setOnClickListener(new View.OnClickListener() {
  316. @Override
  317. public void onClick(View view) {
  318. dialog.dismiss();
  319. }
  320. });
  321. }
  322. }