5

我的应用程序启动取决于具有三个不同复选框的用户偏好:

1-启动没有飞溅和音乐的应用程序。

2-仅启动应用程序。

3-启动带有spalsh和音乐的应用程序。

使用下面的代码可以完美地工作。

但是还有两点要实现:

首先应该选中一个复选框

在您根据需要检查任何一个复选框后第二次返回 mainactivity ,在这里您可以使用后退按钮退出按钮退出应用程序,我已经在我的选项菜单中拥有它,问题是使用后退按钮退出按钮它不响应第一次单击,我必须单击两次才能退出应用程序。

但我达不到,

任何帮助将不胜感激。

public class Splash extends Activity{   
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);  

         SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());                 

         boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
            if (without_splash_screen == true)
            {   
                Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                startActivity(intent);
            }

    boolean splash = getPrefs.getBoolean("splash", true);       
    if(splash == true) {
        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);  
                }
            }                          
        };
        timer.start();   
    }                 

    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 

    SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean music = getPrefs1.getBoolean("splash_music", true);
    if (music == true)      
    ourSong.start();

    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); }}                                 
                                };
         timer.start();   }

@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }

在 xml 文件夹中:prefs.xml

 <?xml version="1.0" encoding="utf-8" ?> 
  <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <CheckBoxPreference android:title="without splash screen" 
              android:defaultValue="true" 
              android:key="without_splash_screen" 
              android:summary="Start app without splash"/> 

    <CheckBoxPreference android:title="splash screen" 
              android:defaultValue="true" 
              android:key="splash" 
              android:summary="Start app with splash only" />

    <CheckBoxPreference android:title="splash screen music" 
              android:defaultValue="true" 
              android:key="splash_music" 
              android:summary="Start app with splash and music" /> 

 </PreferenceScreen>

更新:

我也尝试了下面的代码,但它没有改变任何东西,仍然可以检查所有 chechbox:

public class Splash extends Activity{   
MediaPlayer ourSong;

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);  

     SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());                 

         boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
            if (without_splash_screen == true)               
            {   
                Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                startActivity(intent);                      
            }
    boolean splash = getPrefs.getBoolean("splash", true);       
    if(splash == true) {
        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);
                }
            }                          
        };
        timer.start();   
    }                    
    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 

    SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean music = getPrefs1.getBoolean("splash_music", true);
    if (music == true)      
    ourSong.start();

    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);
                    }
            }
        };                                                                      
         timer.start();  
         }      
public void getPrefs() {  
    SharedPreferences getPrefs = PreferenceManager  
                    .getDefaultSharedPreferences(getBaseContext());  

    boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
    boolean splash = getPrefs.getBoolean("splash", false);       
    boolean music = getPrefs.getBoolean("splash_music", false);

    if (without_splash_screen == true){  
        splash = false; 
        music = false;                  
       }else if (splash == true){  
        music = false; 
        without_splash_screen = false;                       
       }else if (music == true){  
        without_splash_screen = false; 
        splash = false;  
    }
    }
   @Override
   protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
  }
  }

更新:我也尝试了另一个代码,但它没有改变任何东西,仍然可以检查所有 chechbox:

  public class Splash extends Activity{   
    MediaPlayer ourSong;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
      setContentView(R.layout.splash);  

     SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());                 

     boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
        if (without_splash_screen == true)
        {   
            Intent intent = new Intent(Splash.this, MainActivity.class);                                     
            startActivity(intent);
        }
        else {       
            getPrefs.getBoolean("splash", false);       
            getPrefs.getBoolean("splash_music", false);
            }
boolean splash = getPrefs.getBoolean("splash", true);       
if(splash == true) {
    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);  
                }
            } 
        };                             
    timer.start();   
    }                
else
{
    getPrefs.getBoolean("without_splash_screen", false);       
    getPrefs.getBoolean("splash_music", false);
    }
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 
boolean splash_music = getPrefs.getBoolean("splash_music", true);
if (splash_music == true)   {
    ourSong.start();
    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); 
          }
      }                          
  };
  timer.start();   
  }                   
 else
  {
getPrefs.getBoolean("without_splash_screen", false);       
getPrefs.getBoolean("splash", false);
 }
   }
 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  ourSong.release();
  finish();
      } 
   }

任何建议,谢谢。

更新 3(整个项目):

1-选中一个复选框完美实现。

2-在 MainActivity 中使用后退按钮或退出按钮,不响应第一次单击,我必须单击两次或三次才能退出应用程序。

飞溅.java

  public class Splash extends Activity{ 
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle DrTsn) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(DrTsn);
         setContentView(R.layout.splash);  

         SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());                 

         boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
            if (without_splash_screen == true)
            {                                              
                Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                  startActivity(intent);                        
            }

    boolean splash = getPrefs.getBoolean("splash", true);       
    if(splash == true) {
        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);  
                }
            }                          
        };
        timer.start();   
    }                    
    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 

    SharedPreferences getPrefs1 = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean music = getPrefs1.getBoolean("splash_music", true);
    if (music == true)      
    ourSong.start();

    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); 
                  }
            }
        };                                                                      
         timer.start();   
         }  
@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }

首选项.java

 public class Prefs extends PreferenceActivity {
  CheckBoxPreference splash;
  CheckBoxPreference splash_music;
  CheckBoxPreference no_splash_music;

 @SuppressWarnings("deprecation")
 @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;
            }
        });
  }
  }

MainActivity.java

  public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.cool_menu, menu);

getLayoutInflater().setFactory(new Factory() {
public View onCreateView(String name, Context context,AttributeSet attrs) {

if (name .equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);

new Handler().post(new Runnable() {
public void run() {

((TextView) view).setTextSize(25);  
((TextView) view).setTextColor(Color.RED); 
} 
}
);                                        
return view;
}
 catch (InflateException e) {        
 }
  catch (ClassNotFoundException e) {          
  }
}                                  
return null; 
}
}
);           
return super.onCreateOptionsMenu(menu);                                    
}   
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case R.id.aboutUs:
        Intent i = new Intent("com.example.checkbox.ABOUT");
        startActivity(i);
    break;
    case R.id.preferences:
        Intent p = new Intent("com.example.checkbox.PREFS");
        startActivity(p);
    break;
    case R.id.exit:
        finish();
    break;
}
return false;
   }
@Override
public void onBackPressed() {
    finish();
}
   }

关于我们.java

     public class AboutUs extends Activity {
     @Override
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.about);  

      Button button = (Button)findViewById(R.id.about_button);           
    button.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
              finish(); }
                      }
                  );}
                 }
4

3 回答 3

3

“首先应该选中一个复选框。”

不知道如何使这成为一个简短而简单的答案,但请和我在一起,这很有效。

创建您从活动中调用的偏好活动。

在你的 onOptionSelected 中使用类似的东西:

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

这是 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;
                }

            });

   }

 }

并且不要忘记将其添加到清单中:

  <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>

prefs.xml 文件与您发布的文件相同。

如果您有任何错误,请告诉我。我已经测试过了,它在这里工作得很好。

“第二”

不知道为什么你必须点击两次。但这可能是由于飞溅没有完成,只是调用了一个新的意图。在你从初始阶段开始你的主类的地方试试这个:

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

            }

现在,我查看了您的项目,并找出了为什么它有这么多错误。

主要原因:

     if (music == true)     
    ourSong.start();

    Thread timer = new Thread(){

在这里,您缺少 if 语句的括号,导致线程每次都运行。因此,您有两个 MainActivity 同时运行。

其他注意事项:您对所有值都使用“if”。我建议你使用“else if”。

我做了一个小清理。

给你。享受:

链接到 Splash.java

您可以通过多种方式解决最后一个问题。其中之一是:

// 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(); 
于 2013-07-23T02:50:31.907 回答
0

您应该可以将其更改为

    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)
    {
        ....
    }
于 2013-07-13T00:39:58.133 回答
0

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

于 2013-07-18T10:30:01.043 回答