2016-11-20 49 views
0

應用程序中間有一個圖像按鈕,它將打開「夜間模式」和一個用於控制屏幕亮度的seekbaar,但問題在於,圖像按鈕或搜索欄運作和我沒有線索,這是什麼問題。 繼承人的代碼:「夜間模式」代碼無法正常工作

public class Nightmode extends AppCompatActivity { 
    private boolean nightmodeOnOff; 
    public ImageButton modeOnOffButton; 
    private int brightness; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Window window = getWindow(); 
     window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 

     setContentView(R.layout.activity_nightmode); 
     modeOnOffButton = (ImageButton) findViewById(R.id.nightmodeOnOffButton); 
     nightmodeOnOff = false; 


     int prog; 
     //Seekbar 
     SeekBar skbar = (SeekBar) findViewById(R.id.nightModeBar); 
     skbar.setMax(255); 
     skbar.setKeyProgressIncrement(127); 

     try { 
      brightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     skbar.setProgress(brightness); 

     skbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { 
       if (progress <= 25) { 
        brightness = 25; 
       } else { 
        brightness = progress; 
       } 
      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 

      } 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
       android.provider.Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness); 
       WindowManager.LayoutParams lpp = getWindow().getAttributes(); 
       lpp.screenBrightness = brightness/(float)255; 
       getWindow().setAttributes(lpp); 
      } 
     }); 

    } 


    public void nightmodeButtonClicked(View view) { 
     try { 
      if (nightmodeOnOff) { 
       nightmodeOnOff = false; 

       turnNightOff(); 
      } else { 
       nightmodeOnOff = true; 
       turnNightOn(); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void turnNightOn() { 

     try { 
      modeOnOffButton.setImageResource(R.drawable.nightmodeonbutton); 

      findViewById(R.id.activity_nightmode).setBackgroundColor(0x66000000); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void turnNightOff() { 

     try { 
      modeOnOffButton.setImageResource(R.drawable.nightmodeonoffbutton); 
      findViewById(R.id.activity_nightmode).setBackgroundColor(33000000); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     turnNightOff(); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     turnNightOff(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     turnNightOff(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     turnNightOn(); 
    } 
} 

回答