package cn.flightfeather.thirdapp.fragment; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.activity.LoginActivity; import cn.flightfeather.thirdapp.bean.Userinfo; import cn.flightfeather.thirdapp.httpservice.SettingsService; import cn.flightfeather.thirdapp.util.GlobalConfig; import cn.flightfeather.thirdapp.view.SettingCommItemView; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; /** * A simple {@link Fragment} subclass. */ public class SettingFragment extends Fragment implements View.OnClickListener{ private TextView tv_userName; private TextView text_version; private CommonApplication application; private SettingCommItemView sc_changePassword; private SettingCommItemView sc_logOut; private SettingsService settingsService; private Dialog changePWDDialog; public SettingFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_setting, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); application = (CommonApplication) getActivity().getApplication(); settingsService = application.getRetrofit().create(SettingsService.class); tv_userName = (TextView) view.findViewById(R.id.tv_user_name); tv_userName.setText(application.getCurrentUser().getRealname()); text_version = view.findViewById(R.id.text_version); text_version.setText(GlobalConfig.getInstance().getVersionName()); sc_changePassword = (SettingCommItemView) view.findViewById(R.id.sc_edit_password); sc_logOut = (SettingCommItemView) view.findViewById(R.id.sc_logout); sc_logOut.setOnClickListener(this); sc_changePassword.setOnClickListener(this); } private void logOut(){ AlertDialog.Builder dialog = new AlertDialog.Builder(getContext()); dialog.setTitle("要退出登录吗?"); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(getActivity(), LoginActivity.class); startActivity(intent); getActivity().finish(); } }); dialog.setNegativeButton("取消",null); dialog.show(); } private void changePassword(){ final View view2 = View.inflate(getContext(), R.layout.dialog_change_password, null); AlertDialog.Builder dialog = new AlertDialog.Builder(getContext()); dialog.setTitle("修改密码"); dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { EditText et_pwd1 = (EditText) view2.findViewById(R.id.et_pwd1); EditText et_pwd2 = (EditText) view2.findViewById(R.id.et_pwd2); if (et_pwd1.getText().toString().equals(et_pwd2.getText().toString())){ Userinfo user = application.getCurrentUser(); user.setPassword(et_pwd1.getText().toString()); updateUserInfo(user); }else { Toast.makeText(application, "两次密码不一致,请重新输入", Toast.LENGTH_SHORT).show(); } } }); dialog.setView(view2); dialog.setNegativeButton("取消",null); changePWDDialog = dialog.show(); } private void updateUserInfo(Userinfo userinfo){ Call updateUserInfo = settingsService.updateUserInfo(userinfo); updateUserInfo.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body()!=null){ Toast.makeText(application, "修改成功", Toast.LENGTH_SHORT).show(); changePWDDialog.dismiss(); }else { Toast.makeText(application, "修改失败", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call call, Throwable t) { Toast.makeText(application, "联网失败", Toast.LENGTH_SHORT).show(); } }); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.sc_logout: logOut(); // startActivity(new Intent(getActivity(), DebugTransSiteLatlngActivity.class)); break; case R.id.sc_edit_password: changePassword(); break; } } }