0

大约一个星期以来,我一直在用头撞桌子。希望有人可以提供帮助。

这就是我想要做的。

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

  • 如果密码正确,则开始活动,但如果在取消或正确之前不重新显示对话框。

这听起来很简单,但我就是无法理解它。我的代码看起来很垃圾,我知道我一直在剪切和粘贴东西。

我最初是在每个不正确的密码之后调用 getpasswd 方法 oncreate 。Unfortuantley 改变方向时我的窗口泄露了。所以我尝试了这个 oncreatedialog 的东西,我不再有泄漏,但我无法在旋转之间保留我的 edittext 文本框,即使输入不正确,按下“ok”按钮后我也无法重新显示对话框。任何指针将不胜感激。

谢谢。

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(); }
4

2 回答 2

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();
   }
于 2011-11-16T19:43:53.547 回答
0

With how you are doing the password Dialog, you probably need to reload the Activity.

So, if the password is wrong, you would just create an intent, and call the activity again.

Where you check for the correct password, if it is incorrect, try this:

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

startActivity(i);

However, I haven't tried calling an activity using an intent from within that same activity, so you'll have to try it out, but I think it might work.

The way I implemented handling passwords is a bit complicated.

I called my own custom Dialog class from within the onStart method of my PasswordPrompt Activity, like this:

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

In my custom PasswordDialog Dialog class, I declared an defined my own custom interface for handling the result of the password entered. I set a global variable that is used to check if a successful password was given or not.

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

Then, after the password is entered, I defined the ReadyListener interface back in my PasswordPrompt Activity class. If the wrong password is pressed, the dialog is shown again. If cancel is pressed, the application exits.

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

Here is the XML for the layout of my PasswordDialog:

<?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>
于 2011-11-16T19:28:45.433 回答