Java DialogFragment 中的 getContentResolver() 和 getWindow()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20176903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getContentResolver() and getWindow() in a DialogFragment
提问by Mario G.
I'm trying to insert a SeekBar in a Dialog to change the brightness. To do this, I wrote this code.
我试图在对话框中插入一个 SeekBar 来改变亮度。为此,我编写了这段代码。
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();
}
}
The problem is that my IDE Eclipse marks errors in getContentResolver and getWindow because I'm working on a DialogFragment and not an Activity. How can I do to use this instruction in this class? Or, if you can not do that, there is an alternative? Thanks in advance
问题是我的 IDE Eclipse 标记了 getContentResolver 和 getWindow 中的错误,因为我正在处理 DialogFragment 而不是 Activity。我该怎么做才能在这门课中使用这个指令?或者,如果您不能这样做,还有其他选择吗?提前致谢
采纳答案by Raghunandan
http://developer.android.com/reference/android/content/Context.html#getContentResolver()
http://developer.android.com/reference/android/content/Context.html#getContentResolver()
getContentResolver()
needs a context.
getContentResolver()
需要一个上下文。
You have context.getContentResolver()
. Use getActivity().getContentResolver()
.
你有context.getContentResolver()
。使用getActivity().getContentResolver()
.
http://developer.android.com/reference/android/app/Activity.html#getWindow()
http://developer.android.com/reference/android/app/Activity.html#getWindow()
Similarly use getActivity().getWindow()
同样使用 getActivity().getWindow()
getActivity()
getActivity()
Return the Activity this fragment is currently associated with.
返回当前与此片段关联的 Activity。