本文介绍了只有选择飞溅preference checkedboxs一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的应用程序开始依赖于用户的preferance有三种不同checkedbox:无飞溅和音乐1-启动应用程序。飞溅2 - 启动应用程序而已。 3启动应用程序与spalsh和音乐。用C的工作低于$ C $完美。 但仍然是两个点来实现: 第一个只有一个复选框应进行检查。 第二个你检查checkboxs中的任何一个,你以后preFER然后回到mainactivity,在这里你可以退出该应用程序或者通过使用后退按钮或退出按钮我已经在我的选项菜单中,问题是,无论是使用返回按钮或退出按钮它不响应第一点击,我需要点击两次退出应用程序。但我不能达到它,任何帮助,请将AP preciated。 公共类飞溅延伸活动{MediaPlayer的ourSong;@覆盖保护无效的onCreate(包savedInstanceState){ this.requestWindowFeature(Window.FEATURE_NO_TITLE); // TODO自动生成方法存根 super.onCreate(savedInstanceState); 的setContentView(R.layout.splash); 共享preferences的get preFS = preferenceManager.getDefaultShared preferences(getBaseContext()); 布尔without_splash_screen = GET prefs.getBoolean(without_splash_screen,真正的); 如果(without_splash_screen ==真) { 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } 布尔飞溅= GET prefs.getBoolean(扑通一声,真); 如果(闪==真){ 的setContentView(R.layout.splash); 螺纹定时器=新的Thread() { 公共无效的run() { 尝试 { 睡眠(2000年); } 赶上(InterruptedException的E) { e.printStackTrace(); } 最后 { 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } } }; timer.start(); } ourSong = MediaPlayer.create(Splash.this,R.raw.splashsound); 共享preferences的get preFS1 = preferenceManager.getDefaultShared preferences(getBaseContext()); 布尔音乐= GET prefs1.getBoolean(splash_music,真正的); 如果(音乐==真) ourSong.start(); 螺纹定时器=新的Thread(){ 公共无效的run(){ 尝试{ 睡眠(2000年); } 赶上(InterruptedException异常E){ e.printStackTrace(); } 最后{ 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); }} }; timer.start(); }@覆盖保护无效的onPause(){ // TODO自动生成方法存根 super.onPause(); ourSong.release(); 完(); } } 在XML文件夹: prefs.xml &LT; XML版本=1.0编码=UTF-8&GT?; &LT; preferenceScreen的xmlns:机器人=http://schemas.android.com/apk/res/android&GT; &LT;复选框preference机器人:标题=不闪屏 安卓:设置defaultValue =真 机器人:关键=without_splash_screen 机器人:总结=启动的应用程序不会飞溅/&GT; &LT;复选框preference机器人:标题=闪屏 安卓:设置defaultValue =真 机器人:关键=扑通 机器人:摘要= /&GT飞溅只启动应用程序; &LT;复选框preference机器人:标题=闪屏乐 安卓:设置defaultValue =真 机器人:关键=splash_music 机器人:总结=开始与飞溅和音乐应用程序/&GT; &LT; / preferenceScreen&GT; 更新:我想也低于code,但它不会改变任何东西,仍然是所有chechboxs可以检查: 公共类飞溅延伸活动{MediaPlayer的ourSong;@覆盖保护无效的onCreate(包savedInstanceState){ this.requestWindowFeature(Window.FEATURE_NO_TITLE); // TODO自动生成方法存根 super.onCreate(savedInstanceState); 的setContentView(R.layout.splash); 共享preferences的get preFS = preferenceManager.getDefaultShared preferences(getBaseContext()); 布尔without_splash_screen = GET prefs.getBoolean(without_splash_screen,真正的); 如果(without_splash_screen ==真) { 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } 布尔飞溅= GET prefs.getBoolean(扑通一声,真); 如果(闪==真){ 的setContentView(R.layout.splash); 螺纹定时器=新的Thread() { 公共无效的run() { 尝试 { 睡眠(2000年); } 赶上(InterruptedException的E) { e.printStackTrace(); } 最后 { 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } } }; timer.start(); } ourSong = MediaPlayer.create(Splash.this,R.raw.splashsound); 共享preferences的get preFS1 = preferenceManager.getDefaultShared preferences(getBaseContext()); 布尔音乐= GET prefs1.getBoolean(splash_music,真正的); 如果(音乐==真) ourSong.start(); 螺纹定时器=新的Thread(){ 公共无效的run(){ 尝试{ 睡眠(2000年); } 赶上(InterruptedException异常E){ e.printStackTrace(); } 最后{ 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } } }; timer.start(); }公共无效的get preFS(){ 共享preferences的get preFS = preferenceManager .getDefaultShared preferences(getBaseContext()); 布尔without_splash_screen = GET prefs.getBoolean(without_splash_screen,真正的); 布尔飞溅= GET prefs.getBoolean(扑通一声,FALSE); 布尔音乐= GET prefs.getBoolean(splash_music,假); 如果(without_splash_screen ==真){ 飞溅= FALSE; 音乐= FALSE; }否则,如果(闪==真){ 音乐= FALSE; without_splash_screen = FALSE; }否则,如果(音乐==真){ without_splash_screen = FALSE; 飞溅= FALSE; } } @覆盖 保护无效的onPause(){// TODO自动生成方法存根super.onPause();ourSong.release();完(); } } 更新:我想也是另一个code,但它不会改变任何东西,仍然是所有chechboxs可以检查: 公共类飞溅延伸活动{ MediaPlayer的ourSong; @覆盖 保护无效的onCreate(包savedInstanceState){ this.requestWindowFeature(Window.FEATURE_NO_TITLE); // TODO自动生成方法存根 super.onCreate(savedInstanceState); 的setContentView(R.layout.splash); 共享preferences的get preFS = preferenceManager.getDefaultShared preferences(getBaseContext()); 布尔without_splash_screen = GET prefs.getBoolean(without_splash_screen,真正的); 如果(without_splash_screen ==真) { 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } 其他 { GET prefs.getBoolean(扑通一声,FALSE); GET prefs.getBoolean(splash_music,假); }布尔飞溅= GET prefs.getBoolean(扑通一声,真);如果(闪==真){ 的setContentView(R.layout.splash); 螺纹定时器=新的Thread(){ 公共无效的run(){ 尝试{ 睡眠(2000年); } 赶上(InterruptedException异常E){ e.printStackTrace(); } 最后{ 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } } }; timer.start(); }其他{ GET prefs.getBoolean(without_splash_screen,假); GET prefs.getBoolean(splash_music,假); }ourSong = MediaPlayer.create(Splash.this,R.raw.splashsound);布尔splash_music = GET prefs.getBoolean(splash_music,真正的);如果(splash_music ==真){ ourSong.start(); 的setContentView(R.layout.splash);螺纹定时器=新的Thread(){ 公共无效的run(){ 尝试{ 睡眠(2000年); } 赶上(InterruptedException异常E){ e.printStackTrace(); } 最后{ 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } } }; timer.start(); } 其他 {GET prefs.getBoolean(without_splash_screen,假);GET prefs.getBoolean(扑通一声,FALSE); } } @覆盖 保护无效的onPause(){ // TODO自动生成方法存根 super.onPause(); ourSong.release(); 完(); } } 任何意见,谢谢。 更新3(整个项目): -1-酮选中复选框完美实现。 2 - 或者使用后退按钮或退出按钮MainActivity,不先点击反应,我必须单击两次或三次退出应用程序。 Splash.java 公共类飞溅延伸活动{MediaPlayer的ourSong;@覆盖保护无效的onCreate(包DrTsn){ this.requestWindowFeature(Window.FEATURE_NO_TITLE); // TODO自动生成方法存根 super.onCreate(DrTsn); 的setContentView(R.layout.splash); 共享preferences的get preFS = preferenceManager.getDefaultShared preferences(getBaseContext()); 布尔without_splash_screen = GET prefs.getBoolean(without_splash_screen,真正的); 如果(without_splash_screen ==真) { 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } 布尔飞溅= GET prefs.getBoolean(扑通一声,真); 如果(闪==真){ 的setContentView(R.layout.splash); 螺纹定时器=新的Thread() { 公共无效的run() { 尝试 { 睡眠(2000年); } 赶上(InterruptedException的E) { e.printStackTrace(); } 最后 { 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } } }; timer.start(); } ourSong = MediaPlayer.create(Splash.this,R.raw.splashsound); 共享preferences的get preFS1 = preferenceManager.getDefaultShared preferences(getBaseContext()); 布尔音乐= GET prefs1.getBoolean(splash_music,真正的); 如果(音乐==真) ourSong.start(); 螺纹定时器=新的Thread(){ 公共无效的run(){ 尝试{ 睡眠(2000年); } 赶上(InterruptedException异常E){ e.printStackTrace(); } 最后{ 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); } } }; timer.start(); }@覆盖保护无效的onPause(){ // TODO自动生成方法存根 super.onPause(); ourSong.release(); 完(); } } prefs.java 公共类preFS延长preferenceActivity { 复选框preference飞溅; 复选框preference splash_music; 复选框preference no_splash_music; @燮pressWarnings(德precation) @覆盖 保护无效的onCreate(包savedInstanceState){ // TODO自动生成方法存根 super.onCreate(savedInstanceState); 加preferencesFromResource(R.xml preFS); 飞溅=(复选框preference)找到preference(扑通); splash_music =(复选框preference)找到preference(splash_music); no_splash_music =(复选框preference)找到preference(without_splash_screen); splash.setOn preferenceChangeListener(新在preferenceChangeListener(){ @覆盖 公共布尔在preferenceChange(preference preference, 对象为newValue){ // TODO自动生成方法存根 splash.setChecked(真正的); splash_music.setChecked(假); no_splash_music.setChecked(假); 返回true; }});splash_music .setOn preferenceChangeListener(新在preferenceChangeListener(){ @覆盖 公共布尔在preferenceChange(preference preference, 对象为newValue){ // TODO自动生成方法存根 splash.setChecked(假); splash_music.setChecked(真正的); no_splash_music.setChecked(假); 返回true; } });no_splash_music .setOn preferenceChangeListener(新在preferenceChangeListener(){ @覆盖 公共布尔在preferenceChange(preference preference, 对象为newValue){ // TODO自动生成方法存根 splash.setChecked(假); splash_music.setChecked(假); no_splash_music.setChecked(真正的); 返回true; } }); } } MainActivity.java 公共类MainActivity延伸活动{@覆盖保护无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main);}@覆盖公共布尔onCreateOptionsMenu(android.view.Menu菜单){MenuInflater充气= getMenuInflater();inflater.inflate(R.menu.cool_menu,菜单);getLayoutInflater()。setFactory(新厂(){公共查看onCreateView(字符串名称,上下文的背景下,ATTRS的AttributeSet){如果(名称.equalsIgnoreCase(com.android.internal.view.menu.IconMenuItemView)){尝试 {LayoutInflater李= LayoutInflater.from(上下文);最终的视图中查看= li.createView(姓名,空,ATTRS);新的处理程序()。后(新的Runnable(){公共无效的run(){((TextView中)查看).setTextSize(25);((TextView中)查看).setTextColor(Color.RED);}});返回查看;} 赶上(InflateException E){ } 赶上(ClassNotFoundException异常E){ }}返回null;}});返回super.onCreateOptionsMenu(菜单);}@覆盖公共布尔onOptionsItemSelected(菜单项项){ // TODO自动生成方法存根 开关(item.getItemId()){ 案例R.id.aboutUs: 意图I =新的意向书(com.example.checkbox.ABOUT); startActivity(ⅰ); 打破; 案例R.id. preferences: 意图P =新的意向书(com.example.checkbox preFS。); startActivity(对); 打破; 案例R.id.exit: 完(); 打破;}返回false; }@覆盖公共无效onBack pressed(){ 完();} } AboutUs.java 公共类公司简介延伸活动{ @覆盖 保护无效的onCreate(包savedInstanceState){ // TODO自动生成方法存根 super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); 的setContentView(R.layout.about); Button按钮=(按钮)findViewById(R.id.about_button); button.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ 完(); } } );} }解决方案 第一只有一个复选框应进行检查。不知道如何使这个短暂而简单的答案,但挂在身边,这个工程。创建你从你的活动调用preference活动。使用这样的事情在你的onOptionSelected: 情况下R.id. preFS: 意图P =新的意图(MainActivity.this,prefs.class); startActivity(对); 打破; 这是prefs.class 公共类preFS延长preferenceActivity {复选框preference飞溅;复选框preference splash_music;复选框preference no_splash_music;@覆盖保护无效的onCreate(包savedInstanceState){ // TODO自动生成方法存根 super.onCreate(savedInstanceState); 加preferencesFromResource(R.xml preFS); 飞溅=(复选框preference)找到preference(扑通); splash_music =(复选框preference)找到preference(splash_music); no_splash_music =(复选框preference)找到preference(without_splash_screen); splash.setOn preferenceChangeListener(新在preferenceChangeListener(){ @覆盖 公共布尔在preferenceChange(preference preference, 对象为newValue){ // TODO自动生成方法存根 splash.setChecked(真正的); splash_music.setChecked(假); no_splash_music.setChecked(假); 返回true; } }); splash_music .setOn preferenceChangeListener(新在preferenceChangeListener(){ @覆盖 公共布尔在preferenceChange(preference preference, 对象为newValue){ // TODO自动生成方法存根 splash.setChecked(假); splash_music.setChecked(真正的); no_splash_music.setChecked(假); 返回true; } }); no_splash_music .setOn preferenceChangeListener(新在preferenceChangeListener(){ @覆盖 公共布尔在preferenceChange(preference preference, 对象为newValue){ // TODO自动生成方法存根 splash.setChecked(假); splash_music.setChecked(假); no_splash_music.setChecked(真正的); 返回true; } }); } }不要忘了将它添加到清单: &LT;活动 机器人:名称=。com.lordmarty.testforlools preFS 机器人:标签=@字符串/ APP_NAME&GT; &LT;意向滤光器&gt; &lt;作用机器人:名称=。com.lordmarty.testforlools preFS/&GT; &LT;类机器人:名称=android.intent.category.DEFAULT/&GT; &所述; /意图滤光器&gt; &LT; /活性GT;在prefs.xml文件是一样的,你贴的人。让我知道如果你得到任何错误。我测试过它,它工作在这里很好。第二不知道为什么你必须点击两次。但它可能被飞溅没有完成,只需调用一个新的意图引起的。试试这个在任何你开始你的mainclass从飞溅: 终于{ 意向意图=新的意图(Splash.this,MainActivity.class); startActivity(意向); 完(); } 现在,我看过了你的项目,并发现了它为什么有那么越野车。主要的原因: 如果(音乐==真) ourSong.start(); 螺纹定时器=新的Thread(){ 下面你缺少一个括号内为if语句,导致线程每次运行。因此,必须在同一时间运行两个MainActivities。其他注意事项:使用如果的所有值。我建议你使用否则,如果。和我做了一个小的清理工作。在这里你走。享受: 链接到Splash.java您可以解决最后的问题是多方面的。其中之一是这样的: //如果否则,如果添加此下方对方在splass.java声明否则,如果(without_splash_screen ==假放;&安培;飞溅==假放;&功放;音乐== FALSE){//把任何你想要的发生,如果他们没有进行检查。这仅仅是一个例子。的setContentView(R.layout.splash); 的setContentView(R.layout.splash); 螺纹定时器=新的Thread(){ 公共无效的run(){ 尝试 { 睡眠(2000年); }赶上(InterruptedException异常E){ e.printStackTrace(); } 最后 { 意向意图=新的意图(Splash.this, MainActivity.class); startActivity(意向); 完(); } } }; timer.start(); 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- start app with spalsh and music .with the below code its work perfectly .But still two point to be achieved :FIRST only one checkbox should be checked .SECOND after you checked any one of checkboxs as you prefer then go back to mainactivity , here you can exit the app either by use back button or exit button which i already have it in my option menu , the problem is that either using back button or exit button it doesn't respond to first click , i have to click twice to exit the app.but i cant achieve it ,any help please will be appreciated .public class Splash extends Activity{MediaPlayer ourSong;@Overrideprotected 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(); }@Overrideprotected void onPause() { // TODO Auto-generated method stub super.onPause(); ourSong.release(); finish(); } }In xml folder :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:i tried also the below code but it doesn't change any thing , still all chechboxs can be checked :public class Splash extends Activity{MediaPlayer ourSong;@Overrideprotected 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 stubsuper.onPause();ourSong.release();finish(); } }UPDATE:i tried also another code but it doesn't change any thing , still all chechboxs can be checked : 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(); } }Any advice , thanks.UPDATE 3 (WHOLE PROJECT):1- one checkbox checked achieved perfectly .2- either using back button or exit button in MainActivity , doesn't respond to first click , i have to click twice or three times to exit the app.Splash.java public class Splash extends Activity{MediaPlayer ourSong;@Overrideprotected 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(); }@Overrideprotected 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 {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);}@Overridepublic 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);}@Overridepublic 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; }@Overridepublic 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(); } } );} } 解决方案 "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.classpublic class Prefs extends PreferenceActivity {CheckBoxPreference splash;CheckBoxPreference splash_music;CheckBoxPreference no_splash_music;@Overrideprotected 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.javaYou 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.javaelse 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(); 这篇关于只有选择飞溅preference checkedboxs一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-10 15:39