如何使用侦听器中的方法调用 getWindow()
我使用 getwindow 在 onCreate 中的一堆活动开始时设置很多内容。我想用如下方法替换它:
public static void initializeScreen (Activity This){
This.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (global.notBarOnOff == true) {
This.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
This.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);}
//Keeps Screen on
This.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Controls Screen Brightness
Settings.System.putInt(This.getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
final WindowManager.LayoutParams settings = This.getWindow().getAttributes();
settings.screenBrightness = (float) 0.10;
This.getWindow().setAttributes(settings);
}
现在我认为通过调用 SCREEN_Controller.initializeScreen(this) 就可以很好地工作。
稍后我让用户通过按下按钮来更改亮度。
bLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
settings.screenBrightness = (float) 1;
getWindow().setAttributes(settings);
我知道要使用我的initializeScreen,我必须将以下内容添加到我的侦听器中
WindowManager.LayoutParams settings = This.getWindow().getAttributes();
,所以我想用我的 SCREEN_Controller 类中的方法替换它。问题是我无法使用之前使用的(Activity This),因为它不允许我使用 new 来调用 setOnClickListener 中的 Activity。我做错了什么?
I'm using getwindow to set a lot of stuff at the beginning of bunch of my activities within my onCreate. I would like to replace this with a method like the following:
public static void initializeScreen (Activity This){
This.requestWindowFeature(Window.FEATURE_NO_TITLE);
if (global.notBarOnOff == true) {
This.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
This.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);}
//Keeps Screen on
This.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Controls Screen Brightness
Settings.System.putInt(This.getContentResolver(), SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
final WindowManager.LayoutParams settings = This.getWindow().getAttributes();
settings.screenBrightness = (float) 0.10;
This.getWindow().setAttributes(settings);
}
Now I think this will work just fine by calling SCREEN_Controller.initializeScreen(this)
Later I let the user change the brightness by pressing a button.
bLight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
settings.screenBrightness = (float) 1;
getWindow().setAttributes(settings);
I know to use my initializeScreen I would have to add the following to my listener
WindowManager.LayoutParams settings = This.getWindow().getAttributes();
So I would like to replace that with a method as well from my SCREEN_Controller class. The problem is I can't use the (Activity This) that I use previously because it wouldn't let me call the Activity in the setOnClickListener with using new. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样引用内部类中的活动
this
(:-)
):还有两个注意事项:
this
(全部小写),而不是This
。getWindow().getAttributes();
,而不是编写this.getWindow().getAttributes();
。作为一般规则,如果较低范围内没有同名变量的声明,那么您可以在不使用this
的情况下引用该变量。简短的例子:
You can refer to the activity
this
in the internal class like this (:-)
):Two more notes:
this
(all lower case), notThis
.this.getWindow().getAttributes();
, you could just writegetWindow().getAttributes();
. As a general rule, if there was no declaration of a variable with the same name in a lower scope, then you can refer to the variable withoutthis
.Short example: