Only select one checkbox of splash preference checkedboxs

前端 未结 3 2040
北恋
北恋 2021-01-18 23:14

I have app starting depend on user preferance with three different checkedbox :

1- start app without splash and music .

2- start app with splash only .

3条回答
  •  被撕碎了的回忆
    2021-01-18 23:43

    "FIRST only one checkbox should be checked ."

    Not sure how to make this a short and easy answer, but hang with me, this works.

    Create a preference activity that you call from your activity.

    Use something like this in your onOptionSelected:

      case R.id.prefs:
            Intent p = new Intent(MainActivity.this, Prefs.class);
            startActivity(p);
            break;
    

    This is the Prefs.class

    public class Prefs extends PreferenceActivity {
    CheckBoxPreference splash;
    CheckBoxPreference splash_music;
    CheckBoxPreference no_splash_music;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    
        addPreferencesFromResource(R.xml.prefs);
    
    
    
        splash = (CheckBoxPreference) findPreference("splash");
        splash_music = (CheckBoxPreference) findPreference("splash_music");
        no_splash_music = (CheckBoxPreference) findPreference("without_splash_screen");
    
        splash.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    
            @Override
            public boolean onPreferenceChange(Preference preference,
                    Object newValue) {
                // TODO Auto-generated method stub
    
                splash.setChecked(true);
                splash_music.setChecked(false);
                no_splash_music.setChecked(false);
    
                return true;
            }
    
        });
    
        splash_music
                .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    
                    @Override
                    public boolean onPreferenceChange(Preference preference,
                            Object newValue) {
                        // TODO Auto-generated method stub
    
                        splash.setChecked(false);
                        splash_music.setChecked(true);
                        no_splash_music.setChecked(false);
    
                        return true;
                    }
    
                });
    
        no_splash_music
                .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    
                    @Override
                    public boolean onPreferenceChange(Preference preference,
                            Object newValue) {
                        // TODO Auto-generated method stub
    
                        splash.setChecked(false);
                        splash_music.setChecked(false);
                        no_splash_music.setChecked(true);
    
                        return true;
                    }
    
                });
    
       }
    
     }
    

    And don't forget to add it to the Manifest:

      
            
                
    
                
            
        
    

    The prefs.xml file is the same as the one you posted.

    Let me know if you get any errors. I've tested it, and it works fine here.

    "SECOND"

    Not sure why you have to click twice. But it might be caused by the splash not finishing, just calling a new intent. Try this where ever you start your mainclass from the splash:

      finally{
                Intent intent = new Intent(Splash.this,    MainActivity.class);                                     
                startActivity(intent);
                finish();
    
                }
    

    Now, I've looked over your project and found out why it where so buggy.

    Main reason:

         if (music == true)     
        ourSong.start();
    
        Thread timer = new Thread(){
    

    Here you are missing a brackets for the if statement, causing the thread to run everytime. Therefore you have two MainActivities running at the same time.

    Other notes: You use "if"'s for all the values. I suggest you use "else if".

    And I did a small cleanup.

    Here u go. Enjoy:

    Link to Splash.java

    You can solve the last problem in many ways. One of them being this:

    // add this below the other if else if statements in the splass.java
    else if(without_splash_screen == false && splash == false && music == false){
    //put whatever you want to happen if none of them are checked. This is just an example.
    setContentView(R.layout.splash); 
            setContentView(R.layout.splash); 
            Thread timer = new Thread() { 
                public void run() { 
                    try { 
                        sleep(2000); 
                    } catch (InterruptedException e) { 
                        e.printStackTrace(); 
                    } finally { 
                        Intent intent = new Intent(Splash.this, 
                                MainActivity.class); 
                        startActivity(intent); 
                        finish(); 
    
    
                    } 
                } 
            }; 
            timer.start(); 
    

提交回复
热议问题