如果遇到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();
});
答案 0 :(得分: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();
});