我有应用程序启动取决于用户优先与三个不同的复选框:
1-启动应用程序没有飞溅和音乐.
2-启动应用程序只有启动.
3-启动应用程序与spalsh和音乐.
以下代码完美的工作.
但仍有两点要做:
首先只应选中一个复选框.
在你选择了任何一个复选框之后,你可以回到mainactivity,在这里你可以通过使用我在选项菜单中已经拥有它的后退按钮或退出按钮退出应用程序,问题是使用后退按钮或退出按钮它不响应第一次单击,我必须单击两次退出应用程序.
但我无法实现它,
任何帮助,请将不胜感激.
@H_419_22@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(); } }
在xml文件夹中:prefs.xml
@H_419_22@ <?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:
@H_419_22@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(); } }
更新:
我还尝试了另一个代码,但它没有改变任何东西,仍然可以检查所有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中的后退按钮或退出按钮,不响应第一次点击,我必须单击两次或三次才能退出应用程序.
Splash.java
@H_419_22@ 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
@H_419_22@ 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
@H_419_22@ 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
@H_419_22@ 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(); } } );} }
解决方法:
“首先应选中一个复选框.”
不知道如何使这个简短而简单的答案,但与我同在,这是有效的.
创建您从活动中调用的首选项活动.
在onoptionSelected中使用类似的东西:
@H_419_22@ case R.ID.prefs: Intent p = new Intent(MainActivity.this, Prefs.class); startActivity(p); break;
这是Prefs.class
@H_419_22@public 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; } }); } }
并且不要忘记将它添加到Manifest:
@H_419_22@ <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(); }
现在,我已经查看了你的项目,并找出了它为何如此错误.
主要原因:
@H_419_22@ if (music == true) ourSong.start(); Thread timer = new Thread(){
这里缺少if语句的括号,导致线程每次都运行.因此,您有两个MainActivitIEs同时运行.
其他说明:对所有值使用“if”.我建议你使用“else if”.
我做了一个小小的清理工作.
你去吧.请享用:
Link to Splash.java
您可以通过多种方式解决最后一个问题.其中一个是这样的:
@H_419_22@// 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();
总结 以上是内存溢出为你收集整理的android – 只选择一个启动首选项复选框的复选框全部内容,希望文章能够帮你解决android – 只选择一个启动首选项复选框的复选框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)