如何在解雇后显示警告对话框

时间:2011-11-16 18:54:28

标签: android alertdialog orientation-changes

我一直在桌子上打了一个星期。希望有人可以提供帮助。

以下是我要做的事。

  • 显示登录警告对话框。 - 最好我喜欢库存看起来所以alertdialog.builder对我有用。我试过制作一个自定义对话框,但我似乎无法让按钮看起来正确。

  • 如果密码是正确的开始活动,但是如果它没有重新显示对话框,直到取消或更正。

这听起来很直接,但我无法绕过它。我的代码看起来很脏,我知道我一直在剪切和粘贴内容。

我正在调用getpasswd方法oncreate并在每个错误的密码之后调用。在方向改变方面,我的窗户泄漏了。所以我尝试了这个oncreatedialog的东西,我不再得到泄漏,但我不能在旋转之间保持我的edittext文本框,即使输入不正确,我也无法在按下“确定”按钮后重新显示对话框。任何指针都将非常感激。

感谢。

package com.mstamp.dreamhostpal; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class GetLoginPassword extends Activity { public static String MY_PREFS = "Settings"; private static final String TAG = "MyActivity"; String value; String crypto; String text; boolean setup; String cleartxt; boolean cancel_pushed; private static final int ALERT_DIALOG1 = 1; Dialog dialog; //final EditText input = new EditText(this); boolean dismissed = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.setup_password); loadPreferences(); // showDialog(ALERT_DIALOG1); // getPasswd(); } @Override public void onPostCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); showDialog(ALERT_DIALOG1); //getPasswd(); } protected void onPause() { super.onPause(); //dialog.dismiss(); if (cancel_pushed == false) { //EXIT(); } } private void EXIT() { this.finish(); } public void loadPreferences() { int mode = Activity.MODE_PRIVATE; SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, mode); crypto = mySharedPreferences.getString("cryptedAPIKey", null); setup = mySharedPreferences.getBoolean("setup", false); } @Override protected Dialog onCreateDialog(int id) { Dialog dialog; switch(id) { case ALERT_DIALOG1: dialog= getPasswd(); break; default: dialog = null; } return dialog; } private Dialog getPasswd() { Dialog dialog; AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setCancelable(false); alert.setTitle("Login"); // Set an EditText view to get user input final EditText input = new EditText(this); //final EditText editTextPasswordFirst= (EditText)d.findViewById(R.id.EditTextPasswordFirst); input.setHint("Password"); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { value = input.getText().toString(); // Do something with value! if (value != null && value.trim().length() == 0) { Context context = getApplicationContext(); CharSequence text = "Please enter a password."; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); getPasswd(); } else if (value.trim().length() < 5 && value.trim().length() > 0) { Context context = getApplicationContext(); CharSequence text = "The password must be 5 characters or greater."; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); //getPasswd(); dismissed = true; } else { try { if (setup) { cleartxt = CryptoHelper.decrypt(value, crypto); Intent MainCommandsList = new Intent(); MainCommandsList.setClassName("com.mstamp.dreamhostpal", "com.mstamp.dreamhostpal.MainCommandsList"); MainCommandsList.putExtra("cleartxtAPIKey", cleartxt); MainCommandsList.putExtra("cleartxtpassword", value); startActivity(MainCommandsList); } if (!setup) { cleartxt = CryptoHelper.decrypt(value, crypto); Intent GetCommandsMakeDatabase = new Intent(); GetCommandsMakeDatabase.setClassName("com.mstamp.dreamhostpal", "com.mstamp.dreamhostpal.GetCommandsMakeDatabase"); GetCommandsMakeDatabase.putExtra("cleartxtAPIKey", cleartxt); GetCommandsMakeDatabase.putExtra("cleartxtpassword", value); startActivity(GetCommandsMakeDatabase); EXIT(); } } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); Context context = getApplicationContext(); CharSequence text = "That password was incorrect."; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); getPasswd(); } } } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. cancel_pushed = true; EXIT(); } }); dialog = alert.create(); return dialog; //alert.show(); }

2 个答案:

答案 0 :(得分:2)

你可以这样做:

    private void launchLoginDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Enter your username and password");

LayoutInflater factory = LayoutInflater.from(this);
View varianceDialogView = factory.inflate(R.layout.loginDialog,null);

alert.setView(loginDialogView);
alert.setTitle(R.string.loginDialogTitle);

alert.setPositiveButton("login", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {
    if (IsLogincheckOk == false){
        launchLoginDialog();
    }
    else{
        doWhatYouWant();
    }
    }
});

alert.show();
   }

答案 1 :(得分:0)

使用密码对话框的方式,您可能需要重新加载活动。

因此,如果密码错误,您只需创建一个意图,然后再次调用该活动。

如果您检查了正确的密码,如果密码不正确,请尝试以下操作:

Intent i = new Intent(this, GetLoginPassword.class);

startActivity(i);

但是,我没有尝试使用同一活动中的意图调用活动,所以你必须尝试一下,但我认为它可能有用。

我实施处理密码的方式有点复杂。

我在我的PasswordPrompt Activity的onStart方法中调用了我自己的自定义Dialog类,如下所示:

myPWRslt = new OnReadyListener();
objPwPrompt = new PasswordDialog(SecurityPrompt.this, myPWRslt);
objPwPrompt.show();

在我的自定义PasswordDialog Dialog类中,我声明了一个已定义的自定义界面,用于处理输入的密码的结果。我设置了一个全局变量,用于检查是否给出了成功的密码。

protected interface ReadyListener {
  abstract void ready(int iResultCode);

然后,输入密码后,我在PasswordPrompt Activity类中定义了ReadyListener接口。如果按下了错误的密码,则会再次显示该对话框。如果按下取消,则退出应用程序。

private class OnReadyListener implements PasswordDialog.ReadyListener {
    @Override
    public void ready(int iResultCd) {
      try {
        switch (iResultCd) {
        case PasswordDialog.RESULT_LOGIN_SUCCESS_CODE:
          PasswordPrompt.this.setBlPasswordOK(true);
          Intent i = new Intent(PasswordPrompt.this, myMainActivity.class);
          startActivity(i);

          PasswordPrompt.this.finish();
          break;

        case PasswordDialog.RESULT_LOGIN_INCORRECT_CODE:
          PasswordPrompt.this.setBlPasswordOK(false);
          objPWClass.showDialog(iResultCd);
          break;

        case PasswordDialog.RESULT_EXCEEDED_ATTEMPTS_CODE:
          PasswordPrompt.this.setBlPasswordOK(false);
          objPWClass.showDialog(iResultCd);
          break;

        case PasswordDialog.RESULT_LOGIN_CANCELLED_CODE:
          PasswordPrompt.this.setBlPasswordOK(false);
          // exit application
          System.gc();
          PasswordPrompt.this.finish();
          break;

        default:
          break;
        }// end switch
      }// end try
      catch (Exception error) {
        //handle the error
      }// end try/catch (Exception error)
    }// end public void result(boolean success)

  }// end OnReadyListener

以下是我的PasswordDialog布局的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/LinearLayout01" 
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              android:orientation="vertical" >


  <EditText 
        android:id="@+id/txtPwEntry" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text=""
        android:inputType="textPassword"
        android:selectAllOnFocus="true"
        android:hint="Enter your Password"
        android:nextFocusDown="@+id/btnPwSubmit" 
        android:maxLength="25" >
  </EditText>

  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:stretchColumns="*"
        android:background="#000000"
        >
        <TableRow android:background="#000000" android:layout_margin="2dp">
           <LinearLayout
              android:id="@+id/myWidget248"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_marginBottom="5dp"
              >
           <Button 
               android:id="@+id/btnPwSubmit"
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"
               android:layout_weight="0.5"
               android:textStyle="bold"
               android:textSize="14sp"
               android:text="Submit" >
           </Button>
           <Button 
               android:id="@+id/btnPwCancel"
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"
               android:layout_weight="0.5" 
               android:textSize="14sp"
               android:text="Cancel" >
           </Button>
         </LinearLayout>
        </TableRow>
  </TableLayout>
</LinearLayout>