Android(Java):自定义对话框

一、继承DialogFragment

public class LoginDialogFragment extends DialogFragment

二、重写onCreateDialog方法

/**
  *
  * 覆写Fragment类的onCreateDialog方法,在FragmentDialog的show方法执行之后,
  *
  * 系统会调用这个回调方法。
  */
 @Override
 public Dialog onCreateDialog(Bundle saveInstanceState) {

  // 获取对象实例化时传入的窗口标题。
  // int title = getArguments().getInt("title");
  // 返回提醒对话框。
  dialog = new AlertDialog.Builder(getActivity()).create();
  LayoutInflater mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  //gjf
  View view=mInflater.inflate(R.layout.login_idcard_username, null);;
     username_pw_rl = (RelativeLayout) view.findViewById(R.id.username_pw_rl);
     txtIdCard = (EditText) view.findViewById(R.id.txtIdcard);
     txtRealName = (EditText) view.findViewById(R.id.txtRealName);
  if (loginType == 1) {
   txtIdCard.setHint("用 户 名:");
   txtRealName.setHint("密   码:");
            txtRealName.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  } else if (loginType == 2) {
   txtIdCard.setHint("身份证号:");
   txtRealName.setVisibility(View.GONE);
  } else if (loginType == 3) {
   txtIdCard.setHint("身份证号:");
   txtRealName.setHint("姓         名:");
   txtRealName.setVisibility(View.VISIBLE);
  } else if (loginType == 4) {
   txtIdCard.setHint("会计证号:");
   txtRealName.setHint("身份证号:");
         txtRealName.setVisibility(View.VISIBLE);
  }else if (loginType == 5) {
   txtIdCard.setHint("报名序号:");
   txtRealName.setHint("身份证号:");
         txtRealName.setVisibility(View.VISIBLE);
  }else if (loginType == 6) {
   txtIdCard.setHint("卡号:");
   txtRealName.setHint("密码:");
            txtRealName.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
         txtRealName.setVisibility(View.VISIBLE);
  }
  //end
  view.setMinimumHeight(500);
  view.setMinimumWidth(375);
  dialog.setView(view, 0, 0, 0, 0);
  btnOnline = (Button) view.findViewById(R.id.btnLoginOnline);
  btnOnline.setOnClickListener(btnClickListener);
  btnOffline = (Button) view.findViewById(R.id.btnLoginOffline);
  btnOffline.setOnClickListener(btnClickListener);

  txtUserName = (EditText) view.findViewById(R.id.txtUserName);
  txtPassWord = (EditText) view.findViewById(R.id.txtPassWord);
  
  
  txtNotify = (TextView) view.findViewById(R.id.tv_notify);
  txtNotify.setText(this.getDescription());
  cb_remeber = (CheckBox) view.findViewById(R.id.cb_remeberUserName);
     
  
  // 记住账号
  SharedPreferences sp = getActivity().getSharedPreferences("username",
    Context.MODE_PRIVATE);
    //初始化checkbox
  boolean isCheck = sp.getBoolean("ischeck", false);
  if(isCheck){
   cb_remeber.setChecked(true);
  }else{
   cb_remeber.setChecked(false);
  }
  String ac = sp.getString(areaCode, "");
  if (!"".equals(ac)) {
   //gjf
   /*if (loginType == 1) {

   } else if (loginType == 2) {

   } else if (loginType == 3) {
    txtIdCard.setText(sp.getString("idCard", ""));
    txtRealName.setText(sp.getString("realName", ""));
    cb_remeber.setChecked(true);
   } else if (loginType == 4) {

   }*/
          String[] nameAndPw = ac.split("#");
          if(nameAndPw.length>1){
           txtIdCard.setText(nameAndPw[0]);
              txtRealName.setText(nameAndPw[1]);
          }else if(nameAndPw.length==1){
           txtIdCard.setText(nameAndPw[0]);
          }
         
          //end
  }
  int width = LayoutManager.getScreenConstant(getActivity()).displayWidth;
  dialog.show();
  dialog.setCanceledOnTouchOutside(true);
  Window dialogWindow = dialog.getWindow();
  WindowManager.LayoutParams lp = dialogWindow.getAttributes();
  if (width == 2048) {
   lp.y = -30;
   lp.width = 380 * 2;
  } else {
   // lp.y = -30;
   lp.width = 380;
  }
  dialogWindow.setAttributes(lp);
  return dialog;
 }

你可能感兴趣的:(工具类)