2016-06-22 75 views
0

我試圖創建一個Activity,當應用程序啓動時,它將檢查複選框是否被選中。如果選中,我希望它打開第二個Activity,打開NavDrawer而不是當前活動。任何輸入都會有幫助。先謝謝你。複選框啓動數據

package com.example.platinumirish.runassistwithdrawer; 

    import android.content.Intent; 
    import android.os.Bundle; 
    import android.preference.PreferenceManager; 
    import android.support.v7.app.AppCompatActivity; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.CheckBox; 
    import com.example.platinumirish.runassistwithdrawer.IntroPage2Activity; 
    import com.example.platinumirish.runassistwithdrawer.NavDrawer; 
    import com.example.platinumirish.runassistwithdrawer.R; 

    public class MainActivity extends AppCompatActivity { 

    private static final String Startup_Identify = "checkbox_setting"; 

    private CheckBox mCheckBox; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mCheckBox = (CheckBox) findViewById(R.id.checkBox); 

     // Set the initial state of the check box based on saved value 
     mCheckBox.setChecked(isCheckedSettingEnabled()); 

     Button btnOne =(Button)findViewById(R.id.NextP1_P2); 

     btnOne.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent intent = new Intent(getApplicationContext(),IntroPage2Activity.class); 
       startActivity(intent); 

      } 
     }); 

     Button btnTwo =(Button)findViewById(R.id.NextP1_Main); 

     btnTwo.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent intent = new Intent(getApplicationContext(),NavDrawer.class); 
       startActivity(intent); 

      } 
     }); 
    } 


    @Override 
    public void onPause() { 
     super.onPause(); 

     // Persist the setting. Could also do this with an OnCheckedChangeListener. 
     setCheckedSettingEnabled(mCheckBox.isChecked()); 
    } 

    /** 
    * Returns true if the setting has been saved as enabled, 
    * false by default 
    */ 
    private boolean isCheckedSettingEnabled() { 
     return PreferenceManager.getDefaultSharedPreferences(this) 
       .getBoolean(Startup_Identify, false); 
    } 

    /** 
    * Persists the new state of the setting 
    * 
    * @param enabled the new state for the setting 
    */ 
    private void setCheckedSettingEnabled(boolean enabled) { 
     PreferenceManager.getDefaultSharedPreferences(this) 
       .edit() 
       .putBoolean(Startup_Identify, enabled) 
       .apply(); 
    } 
} 
+0

所以你想讓它去其他活動,如果它的檢查? – Shank

+0

是的,我希望它打開一個活動,它打開了NavDrawer而不是當前活動。 –

+0

嗨阿倫波茨,你能解釋什麼是NavDrawer,它是導航抽屜嗎?據我瞭解,當複選框被選中時,你想打開navDrawer處於打開狀態的第二個活動,對吧? – himanshu1496

回答

0

您可以通過,如果選中該複選框檢查和啓動等活動實現這一目標。

private CheckBox mCheckBox; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mCheckBox = (CheckBox) findViewById(R.id.checkBox); 

    // Set the initial state of the check box based on saved value 
    mCheckBox.setChecked(isCheckedSettingEnabled()); 

    if (mCheckBox.isChecked()) { 
     Intent myIntent = new Intent(this, AvitivityName.class); 
     startActivity(myIntent); 
    } 
    //Rest of ur code 
+0

我會試試看看它是否有效。非常感謝你。 –

+0

它的工作原理:D謝謝你的幫助。 –

+0

完成。你走了 –