1

我已经通过此链接https://code.google.com/p/android-lockpattern/在我的应用程序中应用模式功能。但是我遇到了一些问题,我创建了继续按钮以移动到下一个屏幕以确认模式,但它根本不起作用。我希望当用户开始在第一个屏幕上绘制图案时,继续按钮应该开始工作,但它不能正常工作。请告诉我解决方案。我附上了我的 MainActivity.java 代码。提前致谢

      package com.example.lock_pattern;
      import com.example.lock_pattern.prefs.DisplayPrefs;
      public class MainActivity extends Activity {

      private static final String CLASSNAME = MainActivity.class.getName();
      public static final String ACTION_CREATE_PATTERN = CLASSNAME
      + ".create_pattern";


      public static final String ACTION_COMPARE_PATTERN = CLASSNAME
      + ".compare_pattern";


      public static final String ACTION_VERIFY_CAPTCHA = CLASSNAME
      + ".verify_captcha";

 * If you use {@link #ACTION_COMPARE_PATTERN} and the user fails to "login"
      public static final int RESULT_FAILED = RESULT_FIRST_USER + 1;

 * If you use {@link #ACTION_COMPARE_PATTERN} and the user forgot his/ her
      public static final int RESULT_FORGOT_PATTERN = RESULT_FIRST_USER + 2;

      * If you use {@link #ACTION_COMPARE_PATTERN}, and the user fails to "login"
      public static final String EXTRA_RETRY_COUNT = CLASSNAME + ".retry_count";

      * Sets value of this key to a theme in {@code R.style.Alp_Theme_*}. Default
      public static final String EXTRA_THEME = CLASSNAME + ".theme";

      * Key to hold the pattern. It must be a {@code char[]} array.
      public static final String EXTRA_PATTERN = CLASSNAME + ".pattern";

 * You can provide an {@link ResultReceiver} with this key. The activity
       public static final String EXTRA_RESULT_RECEIVER = CLASSNAME
        + ".result_receiver";

 * Put a {@link PendingIntent} into this key. It will be sent before
public static final String EXTRA_PENDING_INTENT_OK = CLASSNAME
        + ".pending_intent_ok";

 * Put a {@link PendingIntent} into this key. It will be sent before
public static final String EXTRA_PENDING_INTENT_CANCELLED = CLASSNAME
        + ".pending_intent_cancelled";

 * You put a {@link Intent} of <i>{@link Activity}</i> into this extra. The
public static final String EXTRA_INTENT_ACTIVITY_FORGOT_PATTERN = CLASSNAME
        + ".intent_activity_forgot_pattern";

 * Helper enum for button OK commands. (Because we use only one "OK" button
private static enum ButtonOkCommand {
    CONTINUE, FORGOT_PATTERN, DONE
}// ButtonOkCommand

 * Delay time to reload the lock pattern view after a wrong pattern.
private static final long DELAY_TIME_TO_RELOAD_LOCK_PATTERN_VIEW = DateUtils.SECOND_IN_MILLIS;

/*
 * FIELDS
 */
private int mMaxRetry;
private boolean mAutoSave;
private IEncrypter mEncrypter;
private int mMinWiredDots;
private ButtonOkCommand mBtnOkCmd;
private Intent mIntentResult;
private int mRetryCount = 0;

/*
 * CONTROLS
 */
private TextView mTextInfo;
private LockPatternView mLockPatternView;
private View mFooter;
private Button mBtnCancel;
private Button mBtnConfirm;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG)
        Log.d(CLASSNAME, "ClassName = " + CLASSNAME);

    /*
     * EXTRA_THEME
     */

    if (getIntent().hasExtra(EXTRA_THEME))
        setTheme(getIntent().getIntExtra(EXTRA_THEME,
                R.style.Alp_Theme_Dark));

    super.onCreate(savedInstanceState);
    Toast.makeText(getApplicationContext(), "oncreate", 7000).show();
    mMinWiredDots = DisplayPrefs.getMinWiredDots(this);
    mMaxRetry = DisplayPrefs.getMaxRetry(this);
    mAutoSave = SecurityPrefs.isAutoSavePattern(this);

    /*
     * Encrypter.
     */
    char[] encrypterClass = SecurityPrefs.getEncrypterClass(this);
    if (encrypterClass != null) {
        try {
            mEncrypter = (IEncrypter) Class.forName(
                    new String(encrypterClass), false, getClassLoader())
                    .newInstance();
        } catch (Throwable t) {
            throw new InvalidEncrypterException();
        }
    }

    mIntentResult = new Intent();
    setResult(RESULT_CANCELED, mIntentResult);

    initContentView();
}// onCreate()

@Override
public void onConfigurationChanged(Configuration newConfig) {
    Log.d(CLASSNAME, "onConfigurationChanged()");
    super.onConfigurationChanged(newConfig);
    initContentView();
}// onConfigurationChanged()

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Toast.makeText(getApplicationContext(), "Inside onKeyDown", Toast.LENGTH_LONG).show();
    if (keyCode == KeyEvent.KEYCODE_BACK
            && ACTION_COMPARE_PATTERN.equals(getIntent().getAction())) {
        /*
         * Use this hook instead of onBackPressed(), because onBackPressed()
         * is not available in API 4.
         */
        finishWithNegativeResult(RESULT_CANCELED);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}// onKeyDown()

@Override
protected void onDestroy() {
    super.onDestroy();
    if (BuildConfig.DEBUG)
        Log.d(CLASSNAME, "onDestroy()");
}// onDestroy()

/**
 * Initializes UI...
 */
private void initContentView() {
    /*
     * Save all controls' state to restore later.
     */

      CharSequence infoText = mTextInfo != null ? mTextInfo.getText() : null;
    Boolean btnOkEnabled = mBtnConfirm != null ? mBtnConfirm.isEnabled()
            : null;
    LockPatternView.DisplayMode lastDisplayMode = mLockPatternView != null ? mLockPatternView
            .getDisplayMode() : null;
    List<Cell> lastPattern = mLockPatternView != null ? mLockPatternView
            .getPattern() : null;

    setContentView(R.layout.activity_main);

    UI.adjustDialogSizeForLargeScreen(getWindow());

    mTextInfo = (TextView) findViewById(R.id.alp_textview_info);
    mLockPatternView = (LockPatternView) findViewById(R.id.alp_view_lock_pattern);

    mFooter = findViewById(R.id.alp_viewgroup_footer);
    mBtnCancel = (Button) findViewById(R.id.alp_button_cancel);
    mBtnConfirm = (Button) findViewById(R.id.alp_button_confirm);

    /*
     * LOCK PATTERN VIEW
     */

    if (getResources().getBoolean(R.bool.alp_is_large_screen)) {
        int size = getResources().getDimensionPixelSize(
                R.dimen.alp_lockpatternview_size);
        LayoutParams lp = mLockPatternView.getLayoutParams();
        lp.width = size;
        lp.height = size;
        mLockPatternView.setLayoutParams(lp);
    }

    /*
     * Haptic feedback.
     */
    boolean hapticFeedbackEnabled = false;
    try {
        hapticFeedbackEnabled = Settings.System.getInt(
                getContentResolver(),
                Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0;
    } catch (Throwable t) {
        /*
         * Ignore it.
         */
    }
    mLockPatternView.setTactileFeedbackEnabled(hapticFeedbackEnabled);

    mLockPatternView.setInStealthMode(DisplayPrefs.isStealthMode(this)
            && !ACTION_VERIFY_CAPTCHA.equals(getIntent().getAction()));
    mLockPatternView.setOnPatternListener(mLockPatternViewListener);
    if (lastPattern != null && lastDisplayMode != null
            && !ACTION_VERIFY_CAPTCHA.equals(getIntent().getAction()))
        mLockPatternView.setPattern(lastDisplayMode, lastPattern);

    /*
     * COMMAND BUTTONS
     */

    if (ACTION_CREATE_PATTERN.equals(getIntent().getAction())) {
        Toast.makeText(getApplicationContext(), "initContentView", 7000).show();
        mBtnCancel.setOnClickListener(mBtnCancelOnClickListener);
        mBtnConfirm.setOnClickListener(mBtnConfirmOnClickListener);

        mBtnCancel.setVisibility(View.VISIBLE);
        mBtnConfirm.setVisibility(View.VISIBLE);
        mFooter.setVisibility(View.VISIBLE);

        if (infoText != null)
            mTextInfo.setText(infoText);
        else
            mTextInfo.setText(R.string.alp_msg_draw_an_unlock_pattern);

        /*
         * BUTTON OK
         */
        if (mBtnOkCmd == null)
            mBtnOkCmd = ButtonOkCommand.CONTINUE;
        switch (mBtnOkCmd) {
        case CONTINUE:
            mBtnConfirm.setText(R.string.alp_cmd_continue);
            break;
        case DONE:
            mBtnConfirm.setText(R.string.alp_cmd_confirm);
            break;
        default:
            /*
             * Do nothing.
             */
            break;
        }
        if (btnOkEnabled != null)
           mBtnConfirm.setEnabled(btnOkEnabled);

    }// ACTION_CREATE_PATTERN
    else if (ACTION_COMPARE_PATTERN.equals(getIntent().getAction())) {
        if (TextUtils.isEmpty(infoText))
            mTextInfo.setText(R.string.alp_msg_draw_pattern_to_unlock);
        else
            mTextInfo.setText(infoText);
        if (getIntent().hasExtra(EXTRA_INTENT_ACTIVITY_FORGOT_PATTERN)) {
            mBtnConfirm.setOnClickListener(mBtnConfirmOnClickListener);
            mBtnConfirm.setText(R.string.alp_cmd_forgot_pattern);
            mBtnConfirm.setEnabled(true);
            mFooter.setVisibility(View.VISIBLE);
        }
    }// ACTION_COMPARE_PATTERN
    else if (ACTION_VERIFY_CAPTCHA.equals(getIntent().getAction())) {
        mTextInfo.setText(R.string.alp_msg_redraw_pattern_to_confirm);

        final ArrayList<Cell> pattern;
        if (getIntent().hasExtra(EXTRA_PATTERN))
            pattern = getIntent()
                    .getParcelableArrayListExtra(EXTRA_PATTERN);
        else
            getIntent().putParcelableArrayListExtra(
                    EXTRA_PATTERN,
                    pattern = LockPatternUtils
                            .genCaptchaPattern(DisplayPrefs
                                    .getCaptchaWiredDots(this)));

        mLockPatternView.setPattern(DisplayMode.Animate, pattern);
    }// ACTION_VERIFY_CAPTCHA
}// initContentView()

 * Encodes {@code pattern} to a string.
private char[] encodePattern(List<Cell> pattern) {

 * Compares {@code pattern} to the given pattern (
private void doComparePattern(List<Cell> pattern) {
    Toast.makeText(getApplicationContext(), "Inside doComparePattern", Toast.LENGTH_LONG).show();
    if (pattern == null)
        return;

    boolean okey = false;

    if (ACTION_COMPARE_PATTERN.equals(getIntent().getAction())) {
        char[] currentPattern = getIntent()
                .getCharArrayExtra(EXTRA_PATTERN);
        if (currentPattern == null)
            currentPattern = SecurityPrefs.getPattern(this);

        okey = Arrays.equals(encodePattern(pattern), currentPattern);
    }// ACTION_COMPARE_PATTERN
    else if (ACTION_VERIFY_CAPTCHA.equals(getIntent().getAction())) {
        final List<Cell> captchaPattern = getIntent()
                .getParcelableArrayListExtra(EXTRA_PATTERN);
        okey = captchaPattern.size() == pattern.size();
        if (okey) {
            for (int i = 0; i < captchaPattern.size(); i++) {
                if (!captchaPattern.get(i).equals(pattern.get(i))) {
                    okey = false;
                    break;
                }
            }// for
        }
    }// ACTION_VERIFY_CAPTCHA

    if (okey)
        finishWithResultOk(null);
    else {
        mRetryCount++;
        mIntentResult.putExtra(EXTRA_RETRY_COUNT, mRetryCount);

        if (mRetryCount >= mMaxRetry)
            finishWithNegativeResult(RESULT_FAILED);
        else {
            mLockPatternView.setDisplayMode(DisplayMode.Wrong);
            mTextInfo.setText(R.string.alp_msg_try_again);
            mLockPatternView.postDelayed(mLockPatternViewReloader,
                    DELAY_TIME_TO_RELOAD_LOCK_PATTERN_VIEW);
        }
    }
}// doComparePattern()

/**
 * Checks and creates the pattern.
 * 
 * @param pattern
 *            the current pattern of lock pattern view.
 */
private void doCheckAndCreatePattern(List<Cell> pattern) {
    Toast.makeText(getApplicationContext(), "Inside doCheckAndCreatePattern", Toast.LENGTH_LONG).show();
    if (pattern.size() < mMinWiredDots) {
        mLockPatternView.setDisplayMode(DisplayMode.Wrong);
        mTextInfo.setText(getResources().getQuantityString(
                R.plurals.alp_pmsg_connect_x_dots, mMinWiredDots,
                mMinWiredDots));
        mLockPatternView.postDelayed(mLockPatternViewReloader,
                DELAY_TIME_TO_RELOAD_LOCK_PATTERN_VIEW);
        return;
    }

    if (getIntent().hasExtra(EXTRA_PATTERN)) {
        if (Arrays.equals(getIntent().getCharArrayExtra(EXTRA_PATTERN),
                encodePattern(pattern))) {
            mTextInfo.setText(R.string.alp_msg_your_new_unlock_pattern);
            mBtnConfirm.setEnabled(true);
        } else {
            mTextInfo.setText(R.string.alp_msg_redraw_pattern_to_confirm);
            mBtnConfirm.setEnabled(false);
            mLockPatternView.setDisplayMode(DisplayMode.Wrong);
            mLockPatternView.postDelayed(mLockPatternViewReloader,
                    DELAY_TIME_TO_RELOAD_LOCK_PATTERN_VIEW);
        }
    } else {
        getIntent().putExtra(EXTRA_PATTERN, encodePattern(pattern));
        mTextInfo.setText(R.string.alp_msg_pattern_recorded);
        mBtnConfirm.setEnabled(true);
    }
}// doCheckAndCreatePattern()

 * Finishes activity with {@link Activity#RESULT_OK}.
private void finishWithResultOk(char[] pattern) {
    Toast.makeText(getApplicationContext(), "Inside finishWithResultOk", Toast.LENGTH_LONG).show();
    if (ACTION_CREATE_PATTERN.equals(getIntent().getAction()))
        mIntentResult.putExtra(EXTRA_PATTERN, pattern);
    else {
        /*
         * If the user was "logging in", minimum try count can not be zero.
         */
        mIntentResult.putExtra(EXTRA_RETRY_COUNT, mRetryCount + 1);
    }

    setResult(RESULT_OK, mIntentResult);

    /*
     * ResultReceiver
     */
    ResultReceiver receiver = getIntent().getParcelableExtra(
            EXTRA_RESULT_RECEIVER);
    if (receiver != null) {
        Bundle bundle = new Bundle();
        if (ACTION_CREATE_PATTERN.equals(getIntent().getAction()))
            bundle.putCharArray(EXTRA_PATTERN, pattern);
        else {
            /*
             * If the user was "logging in", minimum try count can not be
             * zero.
             */
            bundle.putInt(EXTRA_RETRY_COUNT, mRetryCount + 1);
        }
        receiver.send(RESULT_OK, bundle);
    }

    /*
     * PendingIntent
     */
    PendingIntent pi = getIntent().getParcelableExtra(
            EXTRA_PENDING_INTENT_OK);
    if (pi != null) {
        try {
            pi.send(this, RESULT_OK, mIntentResult);
        } catch (Throwable t) {
            if (BuildConfig.DEBUG) {
                Log.e(CLASSNAME, "Error sending PendingIntent: " + pi);
                Log.e(CLASSNAME, ">>> " + t);
                t.printStackTrace();
            }
        }
    }

    finish();
}// finishWithResultOk()

/**
 * Finishes the activity with negative result (
 * {@link Activity#RESULT_CANCELED}, {@link #RESULT_FAILED} or
 * {@link #RESULT_FORGOT_PATTERN}).
 */
private void finishWithNegativeResult(int resultCode) {
    if (ACTION_COMPARE_PATTERN.equals(getIntent().getAction()))
        mIntentResult.putExtra(EXTRA_RETRY_COUNT, mRetryCount);

    setResult(resultCode, mIntentResult);

    /*
     * ResultReceiver
     */
    ResultReceiver receiver = getIntent().getParcelableExtra(
            EXTRA_RESULT_RECEIVER);
    if (receiver != null) {
        Bundle resultBundle = null;
        if (ACTION_COMPARE_PATTERN.equals(getIntent().getAction())) {
            resultBundle = new Bundle();
            resultBundle.putInt(EXTRA_RETRY_COUNT, mRetryCount);
        }
        receiver.send(resultCode, resultBundle);
    }

    /*
     * PendingIntent
     */
    PendingIntent pi = getIntent().getParcelableExtra(
            EXTRA_PENDING_INTENT_CANCELLED);
    if (pi != null) {
        try {
            pi.send(this, resultCode, mIntentResult);
        } catch (Throwable t) {
            if (BuildConfig.DEBUG) {
                Log.e(CLASSNAME, "Error sending PendingIntent: " + pi);
                Log.e(CLASSNAME, ">>> " + t);
                t.printStackTrace();
            }
        }
    }

    finish();
}// finishWithNegativeResult()

/*
 * LISTENERS
 */

private final LockPatternView.OnPatternListener mLockPatternViewListener = new LockPatternView.OnPatternListener() {

    @Override
    public void onPatternStart() {
        Toast.makeText(getApplicationContext(), "Inside onPatternStart", Toast.LENGTH_LONG).show();
        mLockPatternView.removeCallbacks(mLockPatternViewReloader);
        mLockPatternView.setDisplayMode(DisplayMode.Correct);

        if (ACTION_CREATE_PATTERN.equals(getIntent().getAction())) {
            Toast.makeText(getApplicationContext(), "Inside action create pattern", Toast.LENGTH_LONG).show();
            mTextInfo.setText(R.string.alp_msg_release_finger_when_done);
            mBtnConfirm.setEnabled(true);
            if (mBtnOkCmd == ButtonOkCommand.CONTINUE)
                getIntent().removeExtra(EXTRA_PATTERN);
        }// ACTION_CREATE_PATTERN
        else if (ACTION_COMPARE_PATTERN.equals(getIntent().getAction())) {
            mTextInfo.setText(R.string.alp_msg_draw_pattern_to_unlock);
        }// ACTION_COMPARE_PATTERN
        else if (ACTION_VERIFY_CAPTCHA.equals(getIntent().getAction())) {
            mTextInfo.setText(R.string.alp_msg_redraw_pattern_to_confirm);
        }// ACTION_VERIFY_CAPTCHA
    }// onPatternStart()

    @Override
    public void onPatternDetected(List<Cell> pattern) {
        Toast.makeText(getApplicationContext(), "Inside onPatternDetected", Toast.LENGTH_LONG).show();
        if (ACTION_CREATE_PATTERN.equals(getIntent().getAction())) {
            doCheckAndCreatePattern(pattern);
        }// ACTION_CREATE_PATTERN
        else if (ACTION_COMPARE_PATTERN.equals(getIntent().getAction())) {
            doComparePattern(pattern);
        }// ACTION_COMPARE_PATTERN
        else if (ACTION_VERIFY_CAPTCHA.equals(getIntent().getAction())) {
            if (!DisplayMode.Animate.equals(mLockPatternView
                    .getDisplayMode()))
                doComparePattern(pattern);
        }// ACTION_VERIFY_CAPTCHA
    }// onPatternDetected()

    @Override
    public void onPatternCleared() {
        Toast.makeText(getApplicationContext(), "Inside onPatternCleared", Toast.LENGTH_LONG).show();
        mLockPatternView.removeCallbacks(mLockPatternViewReloader);

        if (ACTION_CREATE_PATTERN.equals(getIntent().getAction())) {
            mLockPatternView.setDisplayMode(DisplayMode.Correct);
            mBtnConfirm.setEnabled(true);
            if (mBtnOkCmd == ButtonOkCommand.CONTINUE) {
                getIntent().removeExtra(EXTRA_PATTERN);
                mTextInfo.setText(R.string.alp_msg_draw_an_unlock_pattern);
            } else
                mTextInfo
                        .setText(R.string.alp_msg_redraw_pattern_to_confirm);
        }// ACTION_CREATE_PATTERN
        else if (ACTION_COMPARE_PATTERN.equals(getIntent().getAction())) {
            mLockPatternView.setDisplayMode(DisplayMode.Correct);
            mTextInfo.setText(R.string.alp_msg_draw_pattern_to_unlock);
        }// ACTION_COMPARE_PATTERN
        else if (ACTION_VERIFY_CAPTCHA.equals(getIntent().getAction())) {
            mTextInfo.setText(R.string.alp_msg_redraw_pattern_to_confirm);
            List<Cell> pattern = getIntent().getParcelableArrayListExtra(
                    EXTRA_PATTERN);
            mLockPatternView.setPattern(DisplayMode.Animate, pattern);
        }// ACTION_VERIFY_CAPTCHA
    }// onPatternCleared()

    @Override
    public void onPatternCellAdded(List<Cell> pattern) {
        // TODO Auto-generated method stub
    }// onPatternCellAdded()
};// mLockPatternViewListener

private final View.OnClickListener mBtnCancelOnClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        finishWithNegativeResult(RESULT_CANCELED);
    }// onClick()
};// mBtnCancelOnClickListener

private final View.OnClickListener mBtnConfirmOnClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (ACTION_CREATE_PATTERN.equals(getIntent().getAction())) {
            if (mBtnOkCmd == ButtonOkCommand.CONTINUE) {
                mBtnOkCmd = ButtonOkCommand.DONE;
                mLockPatternView.clearPattern();
                mTextInfo
                        .setText(R.string.alp_msg_redraw_pattern_to_confirm);
                mBtnConfirm.setText(R.string.alp_cmd_confirm);
                mBtnConfirm.setEnabled(false);
            } else {
                final char[] pattern = getIntent().getCharArrayExtra(
                        EXTRA_PATTERN);
                if (mAutoSave)
                    SecurityPrefs.setPattern(MainActivity.this,
                            pattern);
                finishWithResultOk(pattern);
            }
        }// ACTION_CREATE_PATTERN
        else if (ACTION_COMPARE_PATTERN.equals(getIntent().getAction())) {
            /*
             * We don't need to verify the extra. First, this button is only
             * visible if there is this extra in the intent. Second, it is
             * the responsibility of the caller to make sure the extra is an
             * Intent of Activity.
             */
            startActivity((Intent) getIntent().getParcelableExtra(
                    EXTRA_INTENT_ACTIVITY_FORGOT_PATTERN));
            finishWithNegativeResult(RESULT_FORGOT_PATTERN);
        }// ACTION_COMPARE_PATTERN
    }// onClick()
};// mBtnConfirmOnClickListener

/**
 * This reloads the {@link #mLockPatternView} after a wrong pattern.
 */
private final Runnable mLockPatternViewReloader = new Runnable() {

    @Override
    public void run() {
        mLockPatternView.clearPattern();
        mLockPatternViewListener.onPatternCleared();
    }// run()
};// mLockPatternViewReloader

}

4

1 回答 1

0

您的错误在 initContentView() 的初始化中。在将按钮加载到 mBtnConfirm 之前,您正在设置 btnOkEnabled。

将您的 findViewById() 调用移到该方法的顶部,您应该已准备就绪。

于 2013-09-05T15:07:33.693 回答