一個Android 動態提示框事例

public void showLoginDialog(Context context) {
		Log.d(toolsutil.TAG, "showLoginDialog 打開導航頁面");
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		final View myLoginView = layoutInflater.inflate(
				R.layout.activity_wizard, null);
		final Context mcContext = context;
		AlertDialog DlgsetAdmin = new AlertDialog.Builder(this)
				.setTitle("請設定安全號碼")
				.setView(myLoginView)
				.setPositiveButton("肯定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						TextView txtUserNameTextView = (TextView) myLoginView
								.findViewById(R.id.adminnum);
						String txtAdminNum = txtUserNameTextView.getText()
								.toString();
						if (PhoneNumberUtils.isGlobalPhoneNumber(txtAdminNum)) {
							SharedPreferences sharedPreferences = getSharedPreferences(
									"share", MODE_PRIVATE);
							Editor editor = sharedPreferences.edit();
							Log.d(toolsutil.TAG, "showLoginDialog 保存執行狀態");
							editor.putBoolean("isFirstRun", false);
							editor.commit();
							OptParser optParser = new OptParser(mcContext);
							optParser.setAdminNum(txtAdminNum, "sa#");

							try {
								Field field = dialog.getClass().getSuperclass()
										.getDeclaredField("mShowing");
								field.setAccessible(true);
								// 設置mShowing值,欺騙android系統
								field.set(dialog, true);
							} catch (Exception e) {
								Log.d(toolsutil.TAG, e.toString());
							}

						} else {
							// 本處設置保存安全號碼錯誤提示時的對話框鎖定狀態,防止對話框自動關閉
							try {
								Log.d(toolsutil.TAG, "輸入錯誤,鎖定對話框");
								Field field = dialog.getClass().getSuperclass()
										.getDeclaredField("mShowing");
								field.setAccessible(true);
								// 設置mShowing值,欺騙android系統
								field.set(dialog, false);
								Toast.makeText(mcContext, "請輸入正確的安全號碼!",
										Toast.LENGTH_SHORT).show();
							} catch (Exception e) {
								Log.d(toolsutil.TAG, e.toString());
							}

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

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Log.d(toolsutil.TAG, "showLoginDialog 取消設定執行狀態");
						try {
							// 本處首先取消保存安全號碼錯誤提示時的對話框鎖定狀態,而後再關閉對話框
							Field field = dialog.getClass().getSuperclass()
									.getDeclaredField("mShowing");
							field.setAccessible(true);
							// 設置mShowing值,欺騙android系統
							field.set(dialog, true);
							dialog.dismiss();
						} catch (Exception e) {

							Log.d(toolsutil.TAG, e.toString());
						}
					}
				}).create();
		DlgsetAdmin.getWindow().setType(
				WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		DlgsetAdmin.setCanceledOnTouchOutside(false);
		DlgsetAdmin.show();
	}
相關文章
相關標籤/搜索