5

3つの異なるチェックボックスを使用して、ユーザーの好みに応じてアプリを起動しています:

1-スプラッシュと音楽なしでアプリを起動します。

2-スプラッシュのみでアプリを起動します。

3- spalsh と音楽でアプリを起動します。

以下のコードで完全に動作します。

しかし、まだ 2 つのポイントを達成する必要があります。

まず 、チェックボックスを 1 つだけオンにする必要があります。

2番目に、必要に応じてチェックボックスのいずれかをオンにしてからmainactivityに戻ります。ここで、オプションメニューに既にある戻るボタンまたは終了ボタンを使用してアプリを終了できます。問題は、戻るボタン使用するか、終了ボタンは最初のクリックに反応しません。アプリを終了するには 2 回クリックする必要があります。

しかし、私はそれを達成することはできません、

よろしくお願いします。

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>

アップデート:

以下のコードも試してみましたが、何も変わりません。それでも、すべてのチェックボックスをチェックできます。

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

更新: 別のコードも試しましたが、何も変わりません。それでも、すべてのチェックボックスをチェックできます:

  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- 1 つのチェックボックスが完全に達成されました。

2- MainActivity で戻るボタンまたは終了ボタンを使用すると、最初のクリックに応答しません。アプリを終了するには、2 回または 3 回クリックする必要があります。

スプラッシュ.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(); }
                      }
                  );}
                 }
4

3 に答える 3

3

「まずチェックボックスを 1 つだけオンにする必要があります。」

これを短く簡単な答えにする方法がわかりませんが、私と一緒に頑張ってください。これはうまくいきます。

アクティビティから呼び出す設定アクティビティを作成します。

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 ファイルは投稿したものと同じです。

エラーが発生した場合はお知らせください。私はそれをテストしました、そしてそれはここでうまくいきます。

"2番目"

なぜ2回クリックする必要があるのか​​ わかりません。しかし、スプラッシュが終了せず、単に新しいインテントを呼び出していることが原因である可能性があります。スプラッシュからメインクラスを開始する場所でこれを試してください。

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

            }

今、私はあなたのプロジェクトを調べて、なぜバグが多いのかを見つけました。

主な理由:

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

    Thread timer = new Thread(){

ここでは、if ステートメントの括弧が欠落しているため、スレッドが毎回実行されます。したがって、2 つの MainActivities が同時に実行されます。

その他の注意事項: すべての値に「if」を使用します。「else if」を使用することをお勧めします。

そして、ちょっとした大掃除をしました。

どうぞ。楽しみ:

Splash.java へのリンク

最後の問題はさまざまな方法で解決できます。それらの1つはこれです:

// 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 に答える