在活动构建期间满足某些条件时,将调用以下方法。我遇到的问题是虽然吐司确实出现,但警报仍然没有显示。我可以想到两个原因。我的警报构造不正确或UI未更新,因为UI事件不会触发警报创建。问题是什么?有没有什么方法可以在没有UI事件的情况下显示警报对话框?
private void displayAlertToRestorePreviousValues(final Map<String, String> values) {
Toast.makeText(getApplicationContext(), "begin building alert", Toast.LENGTH_LONG).show();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Load Previous Values");
alertDialogBuilder
.setMessage("Click OK to load previous values")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (values != null) {
if (values.get("name") != null) {
nameField.setText(values.get("name"));
}
if (values.get("username") != null) {
usernameField.setText(values.get("username"));
}
if (values.get("email") != null) {
emailField.setText(values.get("email"));
}
} else {
if (prevIntent.hasExtra("name")) {
nameField.setText(prevIntent.getStringExtra("name"));
}
if (prevIntent.hasExtra("username")) {
usernameField.setText(prevIntent.getStringExtra("username"));
}
if (prevIntent.hasExtra("email")) {
emailField.setText(prevIntent.getStringExtra("email"));
}
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Toast.makeText(getApplicationContext(), "end building alert", Toast.LENGTH_LONG).show();
}
修改
我在下面发布我的完整活动代码。
public class RegistrationActivity extends Activity {
private Typeface winInnwa;
private Typeface english;
private TextView registrationText;
private TextView nameText;
private EditText nameField;
private TextView usernameText;
private EditText usernameField;
private TextView emailText;
private EditText emailField;
private TextView confirmText;
private EditText confirmField;
private Button next;
private TextView error;
private TextView regWarning;
private TextView termsConditions;
private RelativeLayout warnTerms;
private boolean nameTouched;
private boolean usernameTouched;
private boolean emailTouched;
private boolean confirmEmailTouched;
private boolean slidedDown;
private boolean isAtPage2;
private boolean testedForPreviousValues;
private Intent prevIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
prevIntent = getIntent();
if (prevIntent.hasExtra("isAtPage2"))
isAtPage2 = prevIntent.getBooleanExtra("isAtPage2", true);
if (isAtPage2) {
Intent intent = new Intent(this, Registration2Activity.class);
startActivity(intent);
}
winInnwa = Typeface.createFromAsset(getAssets(), "fonts/WINNWAB.ttf");
english = Typeface.createFromAsset(getAssets(), "fonts/ARIAL_0.TTF");
registrationText = (TextView) findViewById(R.id.registration_text);
registrationText.setTypeface(english);
nameText = (TextView) findViewById(R.id.name_txt_reg);
nameText.setTypeface(english);
nameField = (EditText) findViewById(R.id.name_fld_reg);
nameField.setTypeface(english);
nameField.setOnFocusChangeListener(new NameFocusChangeListener());
nameField.setOnTouchListener(new NameTouchListener());
usernameText = (TextView) findViewById(R.id.username_txt_reg);
usernameText.setTypeface(english);
usernameField = (EditText) findViewById(R.id.username_fld_reg);
usernameField.setTypeface(english);
usernameField
.setOnFocusChangeListener(new UsernameFocusChangeListener());
usernameField.setOnTouchListener(new UsernameTouchListener());
emailText = (TextView) findViewById(R.id.email_txt_reg);
emailText.setTypeface(english);
emailField = (EditText) findViewById(R.id.email_fld_reg);
emailField.setTypeface(english);
emailField.setOnFocusChangeListener(new EmailFocusChangeListener());
emailField.setOnTouchListener(new EmailTouchListener());
confirmText = (TextView) findViewById(R.id.re_enter_txt_reg);
confirmText.setTypeface(english);
confirmField = (EditText) findViewById(R.id.re_enter_fld_reg);
confirmField.setTypeface(english);
confirmField
.setOnFocusChangeListener(new ConfirmEmailFocusChangeListener());
confirmField.setOnTouchListener(new ConfirmEmailTouchListener());
next = (Button) findViewById(R.id.next_btn);
next.setTypeface(english);
next.setOnClickListener(new OnNextHandler());
error = (TextView) findViewById(R.id.error_messages);
error.setTypeface(english);
regWarning = (TextView) findViewById(R.id.reg_warning_txt);
regWarning.setTypeface(winInnwa);
termsConditions = (TextView) findViewById(R.id.terms_and_conditions_txt);
termsConditions.setTypeface(english);
SpannableString spannableStringAboutInnoLuck = new SpannableString(
getResources().getString(R.string.terms_and_conditions));
spannableStringAboutInnoLuck.setSpan(
new TermsConditionsClickableSpan(), 0,
spannableStringAboutInnoLuck.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringAboutInnoLuck.setSpan(new ForegroundColorSpan(
getResources().getColor(R.color.terms_and_conditions_yellow)),
0, spannableStringAboutInnoLuck.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
termsConditions.setText(spannableStringAboutInnoLuck);
termsConditions.setMovementMethod(LinkMovementMethod.getInstance());
warnTerms = (RelativeLayout) findViewById(R.id.warning_terms_conditions);
testForPreviousValues(savedInstanceState);
}
private void testForPreviousValues(Bundle savedInstanceState) {
Toast.makeText(getApplicationContext(), "testing", Toast.LENGTH_LONG).show();
if (!testedForPreviousValues) {
if (savedInstanceState != null) {
Map<String, String> values = new HashMap<String, String>();
boolean restorationNeeded = false;
if (savedInstanceState.getString("name") != null) {
values.put("name", savedInstanceState.getString("name"));
restorationNeeded = true;
}
if (savedInstanceState.getString("username") != null) {
values.put("username", savedInstanceState.getString("username"));
restorationNeeded = true;
}
if (savedInstanceState.getString("email") != null) {
values.put("email", savedInstanceState.getString("email"));
restorationNeeded = true;
}
if (restorationNeeded) {
displayAlertToRestorePreviousValues(values);
}
} else {
boolean retainingNeeded = false;
if (prevIntent.hasExtra("name")) {
retainingNeeded = true;
}
if (prevIntent.hasExtra("username")) {
retainingNeeded = true;
}
if (prevIntent.hasExtra("email")) {
retainingNeeded = true;
}
if (retainingNeeded) {
displayAlertToRestorePreviousValues();
}
}
}
testedForPreviousValues = true;
}
private void displayAlertToRestorePreviousValues(final Map<String, String> values) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Load Previous Values");
alertDialogBuilder.setMessage("Click OK to load previous values");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
if (values != null) {
if (values.get("name") != null) {
nameField.setText(values.get("name"));
}
if (values.get("username") != null) {
usernameField.setText(values.get("username"));
}
if (values.get("email") != null) {
emailField.setText(values.get("email"));
}
} else {
if (prevIntent.hasExtra("name")) {
nameField.setText(prevIntent.getStringExtra("name"));
}
if (prevIntent.hasExtra("username")) {
usernameField.setText(prevIntent.getStringExtra("username"));
}
if (prevIntent.hasExtra("email")) {
emailField.setText(prevIntent.getStringExtra("email"));
}
}
}
});
alertDialogBuilder.create();
alertDialogBuilder.show();
}
private void displayAlertToRestorePreviousValues() {
displayAlertToRestorePreviousValues(null);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
if (nameField.getText().toString().trim().equals("your real name")
|| nameField.getText().toString().trim().equals("")) {
} else if (usernameField.getText().toString().trim()
.equals("innoLUCK username")
|| usernameField.getText().toString().trim().equals("")) {
} else if (!emailField.getText().toString().trim()
.equals("your email address")
&& !Utils.isEmailValid(emailField.getText().toString())) {
} else if (!emailField.getText().toString().trim()
.equals("your email address")
&& !confirmField.getText().toString().trim()
.equals("confirm email address")
&& !emailField.getText().toString().trim()
.equals(confirmField.getText().toString().trim())) {
} else {
outState.putString("name", nameField.getText().toString().trim());
outState.putString("username", usernameField.getText().toString()
.trim());
if (!usernameField.getText().toString().trim()
.equals("your email address"))
outState.putString("email", emailField.getText().toString()
.trim());
}
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
testForPreviousValues(savedInstanceState);
super.onRestoreInstanceState(savedInstanceState);
}
private void notifyError(String message) {
if (!slidedDown) {
Animation animSlideDown = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_down_ten);
animSlideDown
.setAnimationListener(new SlideDownAnimationListener());
warnTerms.startAnimation(animSlideDown);
}
error.setText("* " + message);
error.setVisibility(View.VISIBLE);
slidedDown = true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.registration, menu);
return true;
}
private class SlideDownAnimationListener implements AnimationListener {
@Override
public void onAnimationEnd(Animation animation) {
warnTerms
.setTranslationY(10 * getResources().getDisplayMetrics().density);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
private class OnNextHandler implements View.OnClickListener {
@Override
public void onClick(View v) {
if (nameField.getText().toString().trim().equals("your real name")
|| nameField.getText().toString().trim().equals("")) {
notifyError("name cannot be blank");
} else if (usernameField.getText().toString().trim()
.equals("innoLUCK username")
|| usernameField.getText().toString().trim().equals("")) {
notifyError("username cannot be blank");
} else if (!emailField.getText().toString().trim()
.equals("your email address")
&& !Utils.isEmailValid(emailField.getText().toString())) {
notifyError("invalid email");
} else if (!emailField.getText().toString().trim()
.equals("your email address")
&& !confirmField.getText().toString().trim()
.equals("confirm email address")
&& !emailField.getText().toString().trim()
.equals(confirmField.getText().toString().trim())) {
notifyError("emails do not match");
} else {
Intent intent = new Intent(RegistrationActivity.this,
Registration2Activity.class);
intent.putExtra("name", nameField.getText().toString().trim());
intent.putExtra("username", usernameField.getText().toString()
.trim());
if (!emailField.getText().toString().trim()
.equals("your email address"))
intent.putExtra("email", emailField.getText().toString()
.trim());
isAtPage2 = true;
startActivity(intent);
}
}
}
private class TermsConditionsClickableSpan extends ClickableSpan {
@Override
public void onClick(View widget) {
Intent intent = new Intent(RegistrationActivity.this,
TermsConditionsActivity.class);
if (prevIntent.hasExtra("name"))
intent.putExtra("name", prevIntent.getStringExtra("name"));
if (prevIntent.hasExtra("username"))
intent.putExtra("username",
prevIntent.getStringExtra("username"));
if (prevIntent.hasExtra("email"))
intent.putExtra("email", prevIntent.getStringExtra("email"));
if (nameField.getText().toString().trim().equals("your real name")
|| nameField.getText().toString().trim().equals("")) {
intent.putExtra("name", nameField.getText().toString().trim());
}
if (usernameField.getText().toString().trim()
.equals("innoLUCK username")
|| usernameField.getText().toString().trim().equals("")) {
intent.putExtra("username", usernameField.getText().toString()
.trim());
}
if (!emailField.getText().toString().trim()
.equals("your email address")
&& !Utils.isEmailValid(emailField.getText().toString())) {
} else if (!emailField.getText().toString().trim()
.equals("your email address")
&& !confirmField.getText().toString().trim()
.equals("confirm email address")
&& !emailField.getText().toString().trim()
.equals(confirmField.getText().toString().trim())) {
} else {
intent.putExtra("email", emailField.getText().toString().trim());
}
intent.putExtra("isAtPage2", false);
startActivity(intent);
}
}
private class ConfirmEmailFocusChangeListener implements
OnFocusChangeListener {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (confirmField.getText().toString().trim()
.equals("confirm email address")
&& confirmEmailTouched) {
confirmField.setText("");
}
} else {
if (confirmField.getText().toString().trim().equals("")) {
confirmField.setText("confirm email address");
}
}
}
}
private class ConfirmEmailTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
confirmEmailTouched = true;
if (confirmField.getText().toString().trim()
.equals("confirm email address")) {
confirmField.setText("");
}
return false;
}
}
private class EmailFocusChangeListener implements OnFocusChangeListener {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (emailField.getText().toString().trim()
.equals("your email address")
&& emailTouched) {
emailField.setText("");
}
} else {
if (emailField.getText().toString().trim().equals("")) {
emailField.setText("your email address");
}
}
}
}
private class EmailTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
emailTouched = true;
if (emailField.getText().toString().trim()
.equals("your email address")) {
emailField.setText("");
}
return false;
}
}
private class UsernameFocusChangeListener implements OnFocusChangeListener {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (usernameField.getText().toString().trim()
.equals("innoLUCK username")
&& usernameTouched) {
usernameField.setText("");
}
} else {
if (usernameField.getText().toString().trim().equals("")) {
usernameField.setText("innoLUCK username");
}
}
}
}
private class UsernameTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
usernameTouched = true;
if (usernameField.getText().toString().trim()
.equals("innoLUCK username")) {
usernameField.setText("");
}
return false;
}
}
private class NameFocusChangeListener implements OnFocusChangeListener {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (nameField.getText().toString().trim()
.equals("your real name")
&& nameTouched) {
nameField.setText("");
}
} else {
if (nameField.getText().toString().trim().equals("")) {
nameField.setText("your real name");
} else if (nameField.getText().toString().trim()
.endsWith("your real name")) {
nameField.setText(nameField
.getText()
.toString()
.trim()
.substring(
0,
nameField.getText().toString().trim()
.length() - 14));
}
}
}
}
private class NameTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
nameTouched = true;
if (nameField.getText().toString().trim().equals("your real name")) {
nameField.setText("");
}
return false;
}
}
private class LoadOldValueListener implements OnTouchListener {
private Map<String, String> values;
public LoadOldValueListener(Map<String, String> values) {
values = this.values;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}
}
答案 0 :(得分:0)
请尝试以下代码。它对我来说很好。请尝试让我知道。感谢。
private void displayAlertToRestorePreviousValues(final Map<String, String> values) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Load Previous Values");
alertDialogBuilder.setMessage("Click OK to load previous values");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
if (values != null) {
if (values.get("name") != null) {
nameField.setText(values.get("name"));
}
if (values.get("username") != null) {
usernameField.setText(values.get("username"));
}
if (values.get("email") != null) {
emailField.setText(values.get("email"));
}
} else {
if (prevIntent.hasExtra("name")) {
nameField.setText(prevIntent.getStringExtra("name"));
}
if (prevIntent.hasExtra("username")) {
usernameField.setText(prevIntent.getStringExtra("username"));
}
if (prevIntent.hasExtra("email")) {
emailField.setText(prevIntent.getStringExtra("email"));
}
}
}
});
alertDialogBuilder.create();
alertDialogBuilder.show();
}