0

android java 编程的异步特性有问题,如何在 onClick 处理程序中引发异常,或者带有警报对话框的方法如何返回值?我必须使用变量来传递结果吗?

  public boolean validAccessCode = false;

//      public boolean requestAccessCode(Activity mActivity) throws Exception {
public void requestAccessCode(Activity mActivity) throws Exception {
  private EditText mPasswordView;

  mPasswordView = new EditText(mActivity);
  mPasswordView.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
  mPasswordView.setTransformationMethod(new PasswordTransformationMethod());

  AlertDialog.Builder alert = new AlertDialog.Builder(mActivity);
  alert.setTitle("Enter access code");
  alert.setView(mPasswordView);

  alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String pw = mPasswordView.getText().toString();
      if(pw.equals(accessCode))
      {
        validAccessCode = true;

         return;
      }

      //throw new Exception("Invalid access code");
    }

    alert.show();
  });
4

1 回答 1

0

你可以在这里使用简单的界面

public interface DialogClickListener {
    public void onClicked(boolean validAccessCode)
}

public void requestAccessCode(Activity mActivity,
      final DialogClickListener  listener) throws Exception {
private EditText mPasswordView;

mPasswordView = new EditText(mActivity);
mPasswordView.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
mPasswordView.setTransformationMethod(new PasswordTransformationMethod());

AlertDialog.Builder alert = new AlertDialog.Builder(mActivity);
alert.setTitle("Enter access code");
alert.setView(mPasswordView);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
  String pw = mPasswordView.getText().toString();

  if (listener != null) {
      listener.onClicked(pw.equals(accessCode));
  }
}

alert.show();

});

于 2013-07-05T04:53:53.820 回答