0

我有一个 AlertDialog 显示何时按下按钮;按下警报“完成”按钮时保存用户名;然后将用户移动到另一个活动。我想弄清楚的是如何仅在第一次按下显示 AlertDialog 的布局按钮时显示 AlertDialog,以便当用户返回应用程序的这一部分并按下相同的布局按钮时,AlertDialog不显示,用户不必再次输入他们的名字。这是不起作用的代码:

    public class CloseoutActivity extends Activity {

SharedPreferences prefs2;
String prefs2String="";
boolean firstRun = true;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_closeout);

 // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
    }


}

public void closeoutCashButtonPressed (View closeoutCashButtonPressedView){

if(firstRun){   

     AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
     builder3.setTitle("    Please enter your full name");
     final EditText input3 = new EditText(this);
     input3.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_FLAG_CAP_WORDS|InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
     builder3.setView(input3);
     builder3.setPositiveButton("Done", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            prefs2String = input3.getText().toString();

            prefs2 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            SharedPreferences.Editor prefs2Editor = prefs2.edit();
            String userName1 = input3.getText().toString();
            prefs2Editor.putString("userName", userName1);
            prefs2Editor.commit();

            launchIntent1();

        }
    });

    builder3.show();
}

firstRun = false;



}

public void launchIntent1(){

    Intent displayCloseoutCashButtonSignal = new Intent (this, CloseoutCashButtonSignal.class);
    displayCloseoutCashButtonSignal.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(displayCloseoutCashButtonSignal);

使用此代码,当我回到应用程序的这一部分时,AlertDialog 会不断弹出。有任何想法吗?谢谢!

4

1 回答 1

0

您需要将您的firstRun标志存储在SharedPrefs或其他一些永久存储中。每次您运行时,Activity您的标志都会true不同。如果这是您存储的唯一位置,username那么您应该能够简单地检查SharedPrefs名称是否存在,如果存在则继续按您的意愿进行,但如果不存在,则可以添加名称并显示Dialog

于 2013-03-02T01:56:51.077 回答