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;
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章