用户登录——注册——密码找回的构造方法

1。第一个案例

/***
	 * 定义弹出View试图
	 * 
	 * @param id
	 * @return
	 */
	static public View getView(int id, Context context) {
		LayoutInflater factory = LayoutInflater.from(context);
		View view = factory.inflate(id, null);
		return view;
	}

	static public void User_Login(Context context, int id) {
		if (common.USERNAME.length() > 0 && common.PASSWORD.length() > 0) {
			return;
		}
		View view = getView(R.layout.more_member_login, context);
		final EditText username = (EditText) view.findViewById(R.id.username);
		final EditText userpass = (EditText) view.findViewById(R.id.userpass);
		final CheckBox passwordsave = (CheckBox) view
				.findViewById(R.id.passwordsave);

		String isSave = "";
		try {
			FileInputStream os = context.openFileInput("issave.dat");
			InputStreamReader inReader = new InputStreamReader(os);
			BufferedReader line = new BufferedReader(inReader);
			String tmpBuffer = "";
			while ((tmpBuffer = line.readLine()) != null) {
				isSave += tmpBuffer;
			}
			os.close();
			inReader.close();
			line.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		String buffer = "";
		try {
			FileInputStream os = context.openFileInput("save.dat");
			InputStreamReader inReader = new InputStreamReader(os);
			BufferedReader line = new BufferedReader(inReader);
			String tmpBuffer = "";
			while ((tmpBuffer = line.readLine()) != null) {
				buffer += tmpBuffer;
			}
			os.close();
			inReader.close();
			line.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Vector list = CommonUtil.Split(buffer, "@@@");
		if (list.size() >= 2) {
			common.TMP_USERNAME = (String) list.elementAt(0);
			common.TMP_PASSWORD = (String) list.elementAt(1);
		}
		username.setText(common.TMP_USERNAME);
		if (isSave.equals("1")) {
			passwordsave.setChecked(true);
			userpass.setText(common.TMP_PASSWORD);
		}
		final Context context2 = context;
		final int id2 = id;
		new AlertDialog.Builder(context).setTitle("用户登录").setView(view)
				.setPositiveButton("立即登陆",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								// common.MAINPAGEACTIVITY = null;
								common.TMP_USERNAME = username.getText()
										.toString().trim();
								common.TMP_PASSWORD = userpass.getText()
										.toString().trim();
								String url = common.SERVER
										+ "/client/login.php?name="
										+ common.TMP_USERNAME + "&pwd="
										+ common.TMP_PASSWORD + "&imei="
										+ common.IMEI + "&platform="
										+ common.PLATFORM + "&version="
										+ common.VERSION + "&cpid="
										+ common.CPID;
								if (context2 instanceof MainPageActivity) {
									((MainPageActivity) context2).url = url;
									((MainPageActivity) context2)
											.showDialog(id2);
								} else if (context2 instanceof FuCai3dActivity) {
									((FuCai3dActivity) context2).url = url;
									((FuCai3dActivity) context2)
											.showDialog(id2);
								}							}
						}).setNeutralButton("免费注册",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface arg0, int arg1) {
								User_Regist(context2, id2);
							}
						}).setNegativeButton("找回密码",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface arg0, int arg1) {
								View view = getView(
										R.layout.more_member_backpassword,
										context2);
								new AlertDialog.Builder(context2).setTitle(
										"找回密码").setView(view).setNeutralButton(
										"确定",
										new DialogInterface.OnClickListener() {

											public void onClick(
													DialogInterface arg0,
													int arg1) {
												arg0.cancel();
												arg0.dismiss();
											}
										}).create().show();

							}
						}).create().show();

		passwordsave.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0, boolean ischecked) {
				String buffer = "0";
				if (ischecked) {
					buffer = "1";
				}
				try {
					FileOutputStream os = context2.openFileOutput("issave.dat",
							Context.MODE_PRIVATE);
					OutputStreamWriter outWriter = new OutputStreamWriter(os);
					outWriter.write(buffer, 0, buffer.length());
					outWriter.flush();
					outWriter.close();
					os.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});
	}

	static public void User_Regist(Context context, int id) {
		View view = getView(R.layout.more_member_registration, context);
		final EditText phone = (EditText) view.findViewById(R.id.phone_number);
		final EditText password = (EditText) view.findViewById(R.id.password);
		final EditText repassword = (EditText) view
				.findViewById(R.id.repassword);

		final Context context2 = context;
		final int id2 = id;
		new AlertDialog.Builder(context).setTitle("用户注册").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {

						String phonenumber = phone.getText().toString();
						String pass = password.getText().toString();
						String repass = repassword.getText().toString();

						if (phonenumber.length() <= 0) {
							Toast.makeText(context2, "手机号码不能为空",
									Toast.LENGTH_SHORT).show();
							return;
						}
						if (phonenumber.length() < 11) {
							Toast.makeText(context2, "手机号码不能少于11位",
									Toast.LENGTH_SHORT).show();
							return;
						}
						if (!phonenumber.substring(0, 1).equals("1")) {
							Toast.makeText(context2, "手机号码第一位数字为1",
									Toast.LENGTH_SHORT).show();
							return;
						}
						if (pass.length() < 6) {
							Toast.makeText(context2, "密码不能小于6位数!",
									Toast.LENGTH_SHORT).show();
							return;
						}

						if (!pass.equals(repass)) {
							Toast.makeText(context2, "密码两次输入不相同!",
									Toast.LENGTH_SHORT).show();
							return;
						}

						common.TMP_USERNAME = phonenumber;
						common.TMP_PASSWORD = pass;

						String url = common.SERVER + "/client/reg.php?name="
								+ phonenumber + "&pwd=" + pass + "&cpid="
								+ common.CPID + "&imei=" + common.IMEI
								+ "&platform=" + common.PLATFORM + "&version="
								+ common.VERSION;
						if (context2 instanceof MainPageActivity) {
							((MainPageActivity) context2).url = url;
							((MainPageActivity) context2).showDialog(id2);
						} else if (context2 instanceof FuCai3dActivity) {
							((FuCai3dActivity) context2).url = url;
							((FuCai3dActivity) context2).showDialog(id2);
						}
					}

				}).setNegativeButton("取消",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface arg0, int arg1) {
								arg0.cancel();
								arg0.dismiss();
							}
						}).create().show();
	}

	static public void LoginStatus(Context context, String buffer) {
		String code = CommonUtil.parseXml(buffer, "code");
		if (code.equals("1")) {
			if (context instanceof MainPageActivity) {
				((MainPageActivity) context).cancel.setVisibility(View.VISIBLE);
				((MainPageActivity) context).logins.setVisibility(View.GONE);
				
			} else if (context instanceof TwoColorActivity) {
				((TwoColorActivity) context).cancel.setVisibility(View.VISIBLE);
				((TwoColorActivity) context).logins.setVisibility(View.GONE);

			} 			}
			// ----------------

			if (common.MAINPAGEACTIVITY != null) {
				common.MAINPAGEACTIVITY.finish();

				Intent intent = new Intent(context, MainPageActivity.class);
				context.startActivity(intent);
			}

			// ----------------
			Toast.makeText(context, "登录成功", Toast.LENGTH_LONG).show();
			common.USERNAME = common.TMP_USERNAME;
			common.PASSWORD = common.TMP_PASSWORD;

			String isSave = "";
			try {
				FileInputStream os = context.openFileInput("issave.dat");
				InputStreamReader inReader = new InputStreamReader(os);
				BufferedReader line = new BufferedReader(inReader);
				String tmpBuffer = "";
				while ((tmpBuffer = line.readLine()) != null) {
					isSave += tmpBuffer;
				}
				os.close();
				inReader.close();
				line.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			buffer = common.USERNAME + "@@@" + common.PASSWORD;
			try {
				FileOutputStream os = context.openFileOutput("save.dat",
						Context.MODE_PRIVATE);
				OutputStreamWriter outWriter = new OutputStreamWriter(os);
				outWriter.write(buffer, 0, buffer.length());
				outWriter.flush();
				outWriter.close();
				os.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			// context.finish();
		} else if (code.equals("-1")) {
			Toast.makeText(context, "未知错误", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-2")) {
			Toast.makeText(context, "手机号码不能为空", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-3")) {
			Toast.makeText(context, "用户名只能是中文英文和数字", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-4")) {
			Toast.makeText(context, "用户名已存在", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-5")) {
			Toast.makeText(context, "用户名不能为空", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-6")) {
			Toast.makeText(context, "用户不存在", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-7")) {
			Toast.makeText(context, "密码错误", Toast.LENGTH_SHORT).show();
		}
	}

	static public void RegisterStatus(Context context, String buffer) {
		String code = CommonUtil.parseXml(buffer, "code");
		if (code.equals("1")) {
			if (context instanceof MainPageActivity) {
				((MainPageActivity) context).cancel.setVisibility(View.VISIBLE);
				((MainPageActivity) context).logins.setVisibility(View.GONE);
				
			} else if (context instanceof TwoColorActivity) {
				((TwoColorActivity) context).cancel.setVisibility(View.VISIBLE);
				((TwoColorActivity) context).logins.setVisibility(View.GONE);

			} }
			
			
			common.MAINPAGEACTIVITY = null;
			Toast.makeText(context, "注册成功!", Toast.LENGTH_SHORT).show();

			common.USERNAME = common.TMP_USERNAME;
			common.PASSWORD = common.TMP_PASSWORD;

			String isSave = "";
			try {
				FileInputStream os = context.openFileInput("issave.dat");
				InputStreamReader inReader = new InputStreamReader(os);
				BufferedReader line = new BufferedReader(inReader);
				String tmpBuffer = "";
				while ((tmpBuffer = line.readLine()) != null) {
					isSave += tmpBuffer;
				}
				os.close();
				inReader.close();
				line.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			buffer = common.USERNAME + "@@@" + common.PASSWORD;
			try {
				FileOutputStream os = context.openFileOutput("save.dat",
						Context.MODE_PRIVATE);
				OutputStreamWriter outWriter = new OutputStreamWriter(os);
				outWriter.write(buffer, 0, buffer.length());
				outWriter.flush();
				outWriter.close();
				os.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (code.equals("-1")) {
			Toast.makeText(context, "未知错误", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-3")) {
			Toast.makeText(context, "用户名含非法字符", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-4")) {
			Toast.makeText(context, "用户名已存在", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
		}
	}

 2。第二个案例,登录是一样的,注册和密码找回不同

/***
	 * 定义弹出View试图
	 * 
	 * @param id
	 * @return
	 */
	static public View getView(int id, Context context) {
		LayoutInflater factory = LayoutInflater.from(context);
		View view = factory.inflate(id, null);
		return view;
	}

	static public void User_Login(Context context, int id) {
		if (common.USERNAME.length() > 0 && common.PASSWORD.length() > 0) {
			return;
		}
		View view = getView(R.layout.more_login, context);
		final EditText username = (EditText) view.findViewById(R.id.username);
		final EditText userpass = (EditText) view.findViewById(R.id.userpass);
		final CheckBox passwordsave = (CheckBox) view
				.findViewById(R.id.passwordsave);
		String isSave = "";
		try {
			FileInputStream os = context.openFileInput("issave.dat");
			InputStreamReader inReader = new InputStreamReader(os);
			BufferedReader line = new BufferedReader(inReader);
			String tmpBuffer = "";
			while ((tmpBuffer = line.readLine()) != null) {
				isSave += tmpBuffer;
			}
			os.close();
			inReader.close();
			line.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		String buffer = "";
		try {
			FileInputStream os = context.openFileInput("save.dat");
			InputStreamReader inReader = new InputStreamReader(os);
			BufferedReader line = new BufferedReader(inReader);
			String tmpBuffer = "";
			while ((tmpBuffer = line.readLine()) != null) {
				buffer += tmpBuffer;
			}
			os.close();
			inReader.close();
			line.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		Vector list = CommonUtil.Split(buffer, "@@@");
		if (list.size() >= 2) {
			common.TMP_USERNAME = (String) list.elementAt(0);
			common.TMP_PASSWORD = (String) list.elementAt(1);
		}
		username.setText(common.TMP_USERNAME);
		if (isSave.equals("1")) {
			passwordsave.setChecked(true);
			userpass.setText(common.TMP_PASSWORD);
		}
		final Context context2 = context;
		final int id2 = id;
		new AlertDialog.Builder(context).setTitle("用户登录").setView(view)
				.setPositiveButton("立即登陆",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								common.TMP_USERNAME = username.getText()
										.toString().trim();
								common.TMP_PASSWORD = userpass.getText()
										.toString().trim();
								String url = common.SERVER
										+ "/client/login.php?name="
										+ common.TMP_USERNAME + "&pwd="
										+ common.TMP_PASSWORD;
								if (context2 instanceof ChatRoomActivity) {
									((ChatRoomActivity) context2).url = url;
									((ChatRoomActivity) context2)
											.showDialog(id2);
								}else if(context2 instanceof MoreMenuActivity) {
									((MoreMenuActivity) context2).url = url;
									((MoreMenuActivity) context2)
											.showDialog(id2);
								}

							}
						}).setNeutralButton("免费注册",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								User_Regist(context2);
							}
						}).setNegativeButton("找回密码",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface arg0, int arg1) {
								User_FindUserPass(context2);

							}
						}).create().show();

		passwordsave.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0, boolean ischecked) {
				String buffer = "0";
				if (ischecked) {
					buffer = "1";
				}
				try {
					FileOutputStream os = context2.openFileOutput("issave.dat",
							Context.MODE_PRIVATE);
					OutputStreamWriter outWriter = new OutputStreamWriter(os);
					outWriter.write(buffer, 0, buffer.length());
					outWriter.flush();
					outWriter.close();
					os.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		});
	}

	static public void User_Regist(final Context context) {
		View view = getView(R.layout.more_regist, context);
		TextView content = (TextView) view.findViewById(R.id.regist_context);

		String url = common.SERVER + "/client/reg.php";
		String buffer = Http.Get(url);
		String mContent = CommonUtil.parseXml(buffer, "c");
		final String number = CommonUtil.parseXml(buffer, "t");

		Vector lines = CommonUtil.Split(mContent, "@@@");
		buffer = "";
		for (int j = 0; j < lines.size(); j++) {
			String line = CommonUtil.SkipSpace((String) lines.elementAt(j));
			line = line.replace("\n", "");
			buffer += line + "\n";
		}
		content.setText(buffer);

		new AlertDialog.Builder(context).setTitle("用户注册").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {

						Uri uri = Uri.parse("tel:" + number);
						// 要转向的目标
						Intent intent = new Intent(Intent.ACTION_CALL, uri);// 拨号打电话,必须要在application中加权限
						context.startActivity(intent);// 启动应用程序
					}

				}).setNegativeButton("取消",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface arg0, int arg1) {
								arg0.cancel();
								arg0.dismiss();
							}
						}).create().show();
	}

	static public void User_FindUserPass(final Context context) {
		View view = getView(R.layout.more_regist, context);
		TextView content = (TextView) view.findViewById(R.id.regist_context);

		String url = common.SERVER + "/client/reg.php?s=1";
		String buffer = Http.Get(url);
		String mContent = CommonUtil.parseXml(buffer, "c");
		final String number = CommonUtil.parseXml(buffer, "t");

		Vector lines = CommonUtil.Split(mContent, "@@@");
		buffer = "";
		for (int j = 0; j < lines.size(); j++) {
			String line = CommonUtil.SkipSpace((String) lines.elementAt(j));
			line = line.replace("\n", "");
			buffer += line + "\n";
		}
		content.setText(buffer);

		new AlertDialog.Builder(context).setTitle("找回密码").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {

						Uri uri = Uri.parse("tel:" + number);
						// 要转向的目标
						Intent intent = new Intent(Intent.ACTION_CALL, uri);// 拨号打电话,必须要在application中加权限
						context.startActivity(intent);// 启动应用程序
					}

				}).setNegativeButton("取消",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface arg0, int arg1) {
								arg0.cancel();
								arg0.dismiss();
							}
						}).create().show();
	}

	static public void LoginStatus(Context context, String buffer) {
		String code = CommonUtil.parseXml(buffer, "code");
		System.out.println(buffer+"===");
		System.out.println(code+"-----");
		if (code.equals("1")) {
			System.out.println("+===========================");
			// ----------------
			Toast.makeText(context, "登录成功", Toast.LENGTH_LONG).show();
			common.USERNAME = common.TMP_USERNAME;
			common.PASSWORD = common.TMP_PASSWORD;

			String isSave = "";
			try {
				FileInputStream os = context.openFileInput("issave.dat");
				InputStreamReader inReader = new InputStreamReader(os);
				BufferedReader line = new BufferedReader(inReader);
				String tmpBuffer = "";
				while ((tmpBuffer = line.readLine()) != null) {
					isSave += tmpBuffer;
				}
				os.close();
				inReader.close();
				line.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			buffer = common.USERNAME + "@@@" + common.PASSWORD;
			try {
				FileOutputStream os = context.openFileOutput("save.dat",
						Context.MODE_PRIVATE);
				OutputStreamWriter outWriter = new OutputStreamWriter(os);
				outWriter.write(buffer, 0, buffer.length());
				outWriter.flush();
				outWriter.close();
				os.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (code.equals("-1")) {
			Toast.makeText(context, "未知错误", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-2")) {
			Toast.makeText(context, "手机号码不能为空", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-3")) {
			Toast.makeText(context, "用户名只能是中文英文和数字", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-4")) {
			Toast.makeText(context, "用户名已存在", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-5")) {
			Toast.makeText(context, "用户名不能为空", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-6")) {
			Toast.makeText(context, "用户不存在", Toast.LENGTH_SHORT).show();
		} else if (code.equals("-7")) {
			Toast.makeText(context, "密码错误", Toast.LENGTH_SHORT).show();
		}
	}

 

你可能感兴趣的:(android—login)