2013-11-24 12 views
5

Sto tentando di inserire un SeekBar in una finestra di dialogo per modificare la luminosità. Per fare questo, ho scritto questo codice.getContentResolver() e getWindow() in un DialogFragment

public class Lum extends DialogFragment{ 


     private SeekBar brightbar; 
     private int brightness; 
     private ContentResolver cResolver; 
     private Window window; 
     TextView txtPerc; 
     boolean stato; 
     Context context; 

     public android.app.Dialog onCreateDialog(Bundle savedInstanceState) { 
      // Use the Builder class for convenient dialog construction 

      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

      LayoutInflater inflater = getActivity().getLayoutInflater(); 
      View view = inflater.inflate(R.layout.dialog_brightness, null); 
      builder.setView(view); 

     brightbar = (SeekBar) view.findViewById(R.id.brightbar); 
     txtPerc = (TextView) view.findViewById(R.id.txtPercentage); 
     //cResolver = getContentResolver(); 
     //window = getWindow(); 
     brightbar.setMax(255); 
     brightbar.setKeyProgressIncrement(1); 

     brightbar = (SeekBar) view.findViewById(R.id.brightbar); 
     txtPerc = (TextView) view.findViewById(R.id.txtPercentage); 
     cResolver = getContentResolver(); 
     window = getWindow(); 
     brightbar.setMax(255); 
     brightbar.setKeyProgressIncrement(1); 

     try { 
      brightness = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); 
     } catch (SettingNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     float perc = (brightness /(float)255)*100; 
     txtPerc.setText((int)perc +" %"); 

     brightbar.setProgress(brightness); 
     brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
     { 
      public void onStopTrackingTouch(SeekBar seekBar) 
      { 
       Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness); 
       LayoutParams layoutpars = window.getAttributes(); 
       layoutpars.screenBrightness = brightness/(float)255; 
       window.setAttributes(layoutpars); 
      } 

      public void onStartTrackingTouch(SeekBar seekBar) 
      { 

      } 

      public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
      { 


        brightness = progress; 

        float perc = (brightness /(float)255)*100; 
        txtPerc.setText((int)perc +" %"); 
      } 
     });  

      builder.setTitle("Brightness") 
        .setPositiveButton("Close", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dismiss(); 
    } 
        }); 

      // Create the AlertDialog object and return it 
      return builder.create(); 
     } 
    } 

Il problema è che il mio Eclipse segna gli errori in getContentResolver e getWindow perché sto lavorando su un DialogFragment e non di un'attività. Come posso fare per usare questa istruzione in questa classe? Oppure, se non puoi farlo, c'è un'alternativa? Grazie in anticipo

+0

utilizzare 'getActivity(). GetContentResolver()' – Raghunandan

+0

Ciao, grazie per la risposta. Funziona. Ho usato anche getActivity(). GetWindow() e funziona. Grazie Manny. –

risposta

18

http://developer.android.com/reference/android/content/Context.html#getContentResolver()

getContentResolver() ha bisogno di un contesto.

Hai context.getContentResolver(). Utilizzare getActivity().getContentResolver().

http://developer.android.com/reference/android/app/Activity.html#getWindow()

Allo stesso modo utilizzare getActivity().getWindow()

getActivity()

Ritorna l'attività questo frammento è attualmente associato.

+0

@ user3027663 siete i benvenuti. puoi controllare i documenti per maggiori informazioni – Raghunandan

+0

Cool !! Grazie mille. Pensi che potresti aiutarmi con questa domanda: http://stackoverflow.com/questions/25549679/how-can-i-split-a-long-single-sqliteopenhelper-into-serveral-classes-one-for-e – eddy

Problemi correlati