2016-06-22 41 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

完成。你走了 –