2013-07-13 23 views
4

我的應用程序啓動依賴於用戶偏好研究用三種不同的checkedbox:只能選擇閃偏好的一個複選框checkedboxs

1-開始應用,但不飛濺和音樂。

2-啓動應用程序只有飛濺。

3-啓動spalsh和音樂的應用程序。

與下面的代碼完美的工作。

但仍然是兩個點來實現:

FIRST只有一個複選框應進行檢查。

第二你檢查checkboxs中的任何一個後,根據自己的喜好再回去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> 

UPDATE:

我也嘗試下面的代碼,但它不會改變任何東西,仍然是所有chechboxs可以被檢查:

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

UPDATE: 我也嘗試另一個代碼,但它不會改變任何東西,仍然是所有chechboxs可以檢查:

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

任何意見,謝謝。

UPDATE 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(); 
      } 
     } 

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

AboutUs.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(); } 
         } 
       );} 
       } 
+0

相反的複選框就可以使用單選按鈕與無線電集團小工具,將幫助您選擇從他們三人中的一個選項。如果你想讓它看起來像checkBox那樣爲選中和未選中的模式創建一個可繪製的xml文件並將其應用於所有單選按鈕的button屬性。 –

回答

2

「FIRST只有一個複選框應進行檢查。」

不知道如何使這個簡短和容易的答案,但與我一起,這工程。

創建您從活動中調用的偏好活動。

使用這樣的事情在你的onOptionSelected:

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

這是偏好設置。類

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文件是一樣的,你貼一個。

讓我知道你是否有任何錯誤。我測試過了,它在這裏工作得很好。

「第二」

不知道爲什麼你必須點擊兩次。但這可能是由於飛濺沒有完成而引起的,只是調用了新的意圖。 試試這個在任何你從飛濺啓動mainclass:

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

      } 

現在,我看過了你的項目,發現它爲什麼有那麼馬車。

的主要原因是:

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

    Thread timer = new Thread(){ 

這裏你缺少一個括號內爲if語句,導致線程每次運行。因此,您有兩個MainActivities同時運行。

其他注意事項:對所有值使用「if」。我建議你使用「其他如果」。

我做了一個小清理。

這裏你去。享受:

Link to 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(); 
+0

謝謝你的答案,首先只有一個複選框應該檢查(完美工作),但問題的第二部分仍然沒有實現,我也用 public void onBackPressed(){ finish(); }它什麼都不做,請任何意見。 – androidqq6

+0

我無法爲第二部分提供幫助,因爲我的任何設備都沒有這個問題。你使用什麼設備? – LordMarty

+0

,你的應用中是否還有其他類?對我們隱藏? 就像,當你回頭一次,它看起來像關閉了一項活動,但你還有一個關閉? – LordMarty

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) 
    { 
     .... 
    } 
+0

我運行的應用程序,它啓動時沒有音樂的啓動畫面,但堆疊在飛濺,而不是去下一個活動 – androidqq6

+0

發佈更新的代碼,所以我們知道你在用什麼 – codeMagic

+0

如果你希望他們能夠檢查只有一個,那麼爲什麼不是使用'RadioGroup'而不是'CheckBox'es? – codeMagic

0

我實現偏好活動,看起來像複選框,如下所示內部單選按鈕..

enter image description here

只能有一個選擇一次。 我做到了這一點使用2多個XML文件

enter image description here

一個內部繪製(check_dyn.xml)和另一內部佈局(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> 

現在,偏好,(這裏的prefs.xml)內,加偏好,並設置其佈局屬性。即

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

注:我不喜歡這樣的做法,因爲它很難獲得單個值。

我喜歡 - >

From settings enter image description here

+0

((很難獲得個人價值。)),這是即時通訊新的Android的點,我知道你的解決方案之前,我嘗試它,但我不能得到的價值作爲執行其行動 – androidqq6

相關問題