Only select one checkbox of splash preference checkedboxs

前端 未结 3 2041
北恋
北恋 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:

      <activity
            android:name="com.lordmarty.testforlools.Prefs"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.lordmarty.testforlools.PREFS" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    

    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(); 
    
    0 讨论(0)
  • 2021-01-18 23:49

    You should just be able to change it to

        boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
        boolean splash = getPrefs.getBoolean("splash", true);
        boolean splash_music = getPrefs.getBoolean("splash_music", true);
    
        if (without_splash_screen)
        {   
            ...
        }
        else if (splash)
        {
             ...
        }
        else if (splash_music)
        {
            ....
        }
    
    0 讨论(0)
  • 2021-01-19 00:07

    I implemented Radio buttons inside preference activity that looks like check boxes as follows..

    enter image description here

    Only one can be selected at a time. I done this using 2 more xml files

    enter image description here

    One inside drawable(check_dyn.xml) and another inside layout(radiochecks.xml) .

    check_dyn.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">   
        <item android:state_checked="false"
              android:state_enabled="true"
              android:drawable="@android:drawable/checkbox_off_background" />
        <item android:state_checked="true"
              android:state_enabled="true"
              android:drawable="@android:drawable/checkbox_on_background" />
    </selector>
    

    radiochecks.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/check_dyn"
                android:checked="true"
                android:text="RadioButton1" />
    
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/check_dyn"
                android:text="RadioButton2" />
    
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/check_dyn"
                android:text="RadioButton3" />
        </RadioGroup>
    
    </LinearLayout>
    

    Now, inside the preference,(here prefs.xml), add a preference and set its layout property. ie,

    <Preference android:layout="@layout/radiochecks"
        />
    

    Note: I don't prefer a practice like this, since it is hard to get the individual values.

    I prefer ->

    From settings enter image description here

    0 讨论(0)
提交回复
热议问题