2014-04-01 16 views
0

我正在製作一個程序,其中我想使用一個Activity 作爲啓動畫面,其中顯示了我正在使用的指令列表,作爲背景圖像,複選框和按鈕。 我希望當CheckBoxclickedonChecked和 然後我點擊按鈕,該活動不應該在啓動時再次看到 。使用共享參數,複選框和按鈕

enter image description here

這是我在做什麼,但還是沒有用

CheckBox cb; 
SharedPreferences sp; 
Button btn; 
int result; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btn = (Button) findViewById(R.id.button1); 

    cb = (CheckBox) findViewById(R.id.checkBox1); 
    sp = (SharedPreferences) PreferenceManager 
      .getDefaultSharedPreferences(this); 
    OnCheckedChangeListener cb1 = new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      // TODO Auto-generated method stub 
      if(cb.isChecked()){ 



      } 
      else{ 


       result = sp.getInt("showActivity", -1); 
       if(result == 0){ 
        Intent i = new Intent(); 
        i.setClass(MainActivity.this, SecondActivity.class); 
        startActivity(i); 
       } 
      } 
     } 
    }; 
    cb.setOnCheckedChangeListener(cb1); 
    btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      SharedPreferences.Editor editor = sp.edit(); 
      editor.putBoolean("showActivity",cb.isChecked()); 
      editor.commit(); 
      Toast.makeText(getApplicationContext(), "dgdgd", Toast.LENGTH_LONG).show(); 
      Intent i = new Intent(); 
      i.setClass(MainActivity.this, SecondActivity.class); 
      startActivity(i); 

     } 
    }); 
} 
+0

這是你的主要/初始活動? – RobinHood

+0

這是一個飛濺的活動,我希望當用戶點擊複選框時,不應該再次看到這個活動,如果他沒有點擊ckeckbox,這個活動應該在啓動時再次看到 – Karma5

+0

但是你有什麼邏輯?你可以發佈它 – km86

回答

1

基本上你需要使用Share-Preference來實現,把條件中的onCreate是user checked initially or not並調用相應的屏幕按布爾值。嘗試下面的代碼。

public class SplashScreen extends Activity { 

    SharedPreferences pref; 
    boolean state; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     pref = getSharedPreferences("PackageName", Context.MODE_PRIVATE); 
     state= pref.getBoolean("State", false); 

     if(state){ 
      Intent intent= new Intent(SplashScreen.this,NextActivity.class); 
      startActivity(intent); 
      finish(); 
     }else{ 

      setContentView(R.layout.fragment_main); 



     CheckBox chk = (CheckBox)findViewById(R.id.checkBox1); 
     Button btn =(Button)findViewById(R.id.button1); 


     chk.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked){ 
        state=true; 
       }else{ 
        state=false; 
       } 


      } 
     }); 

     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Editor edit= pref.edit(); 
       edit.putBoolean("State", state); 
       edit.commit(); 

       Intent intent= new Intent(SplashScreen.this,NextActivity.class); 
       startActivity(intent); 


      } 
     }); 

     } 

    } 



} 
+0

謝謝你soo :-) – Karma5

+0

現在,如果我想回到使用意圖的SplashScreen活動,它不會發生? – Karma5

+0

哦,如果複選框沒有被選中,那麼用戶可以回到飛濺,是嗎? – RobinHood

0
CheckBox cb; 
SharedPreferences sp; 
Button btn; 
// never used 
//int result; 
private final String DONT_SHOW_ACTIVITY = "dontShowActivity"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    sp = (SharedPreferences) PreferenceManager 
      .getDefaultSharedPreferences(this); 
    // default value is false, means you will start MainActivity at first time. 
    // And if you hava checked "dont show again", MainActivity will finish itself 
    // immediately, and start the SecondActivity. 
    if(sp.getBoolean(DONT_SHOW_ACTIVITY, false)){ 
     Intent i = new Intent(); 
     i.setClass(MainActivity.this, SecondActivity.class); 
     startActivity(i); 
     this.finish();  
    }   
    setContentView(R.layout.activity_main); 
    cb = (CheckBox) findViewById(R.id.checkBox1); 
    btn = (Button) findViewById(R.id.button1); 
    btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      SharedPreferences.Editor editor = sp.edit(); 
      editor.putBoolean(DONT_SHOW_ACTIVITY,cb.isChecked()); 
      editor.commit(); 
      Toast.makeText(getApplicationContext(), "dgdgd", Toast.LENGTH_LONG).show(); 
      Intent i = new Intent(); 
      i.setClass(MainActivity.this, SecondActivity.class); 
      startActivity(i); 
      // you should finish MainActivity here, or you can press back return from 
      // SecondActivity to MainActivity. 
      this.finish() 
    } 
}); 

}