1

这是我的代码。我有一个文本视图和 2 个按钮接受和拒绝。当用户单击接受按钮时,我使用共享首选项将状态保存为 100。

下次用户登录时,我需要检查用户是否已经点击了接受按钮。如果他已经接受了,那我应该去家庭活动。

用户单击接受后,我不需要再次显示此活动。

public int kill;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Toast.makeText(Eula.this, "Status of the app is "+kill, Toast.LENGTH_LONG).show();
    if(kill==100)
    {
    Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("mobi.infoways.triviavs1_0","mobi.infoways.triviavs1_0.Home"));
        startActivity(intent);
    }
    setContentView(R.layout.eulatxt);
    Intent i2 = getIntent();                
    addListenerOnButton();
}
        private void addListenerOnButton() {

        TextView t = (TextView) findViewById(R.id.txtv1); 
        t.setText(f);
        Button Accept = (Button) findViewById(R.id.btn1);    
            Accept.setOnClickListener(startListener);
            Button Reject = (Button) findViewById(R.id.btn2);    
            Reject.setOnClickListener(startListener);
}

        OnClickListener startListener = new OnClickListener() 
        {
            public void onClick(View v) {

          switch (v.getId()) {
             case R.id.btn1:

                 SharedPreferences prefs = getPreferences(MODE_PRIVATE);
                 SharedPreferences.Editor editor = prefs.edit();
                 editor.putInt("storedInt",100); 
                 editor.commit();
                     kill = prefs.getInt("storedInt", 100);
                    Toast.makeText(Eula.this, "status =" + kill, Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setComponent(new ComponentName("mobi.infoways.triviavs1_0","mobi.infoways.triviavs1_0.Home"));
                startActivity(intent);
              break;
             case R.id.btn2:
                 Toast.makeText(Eula.this, "button 2 clicked", Toast.LENGTH_SHORT).show();
          Eula.this.finish();
              break;
          }  

    };

}
4

3 回答 3

1

参考下面的代码

SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Boolean checkforFirstTime;

sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();

checkforFirstTime = sharedPreferences.getBoolean("checkforFirstTime", true);

//Checking whether application is launched first time
if (checkforFirstTime) {

    //your code
    editor.putBoolean("checkforFirstTime", false);
    editor.commit();

 } else {

 }
于 2012-07-31T07:13:30.330 回答
1

代替

SharedPreferences prefs = getPreferences(MODE_PRIVATE);

利用

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(YourAcitivyName.this);

同样在onCreate()构造super函数加载首选项并初始化kill变量之后。

在 super.onCreate(savedInstanceState) 旁边的 onCreate 方法中初始化 kill;本身可能就足够了..

于 2012-07-31T06:57:10.497 回答
0

当加载共享首选项时调用:

SharedPreferences save = getSharedPreferences("login", Activity.MODE_PRIVATE);
save.getInt(variable, 0); // zero is default value if you dont have it yet saved

保存时调用:

SharedPreferences settings = getSharedPreferences("login", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("storedInt", 100);
editor.commit();
于 2012-07-31T07:18:55.367 回答