如何使用侦听器中的方法调用 getWindow()

发布于 2024-12-02 01:38:00 字数 1526 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2024-12-09 01:38:00

您可以像这样引用内部类中的活动 this ( :-) ):

YourClassName.this

还有两个注意事项:

  • 在 Java 中,有 this (全部小写),而不是 This
  • 您可以只编写 getWindow().getAttributes();,而不是编写 this.getWindow().getAttributes();。作为一般规则,如果较低范围内没有同名变量的声明,那么您可以在不使用 this 的情况下引用该变量。

简短的例子:

public class DontMissTwice extends Activity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shaving);
        TextView knife = (TextView)findViewById(R.id.knife);
        knife.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View.OnClickListener thisListener = this; // refers to View.OnClickListener's object
                DontMissTwice thisDontMissTwice = DontMissTwice.this // refers to DontMissTwice's object
            }
        });
    }
}

You can refer to the activity this in the internal class like this ( :-) ):

YourClassName.this

Two more notes:

  • In Java, there's this (all lower case), not This.
  • Instead of writing this.getWindow().getAttributes();, you could just write getWindow().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 without this.

Short example:

public class DontMissTwice extends Activity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shaving);
        TextView knife = (TextView)findViewById(R.id.knife);
        knife.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View.OnClickListener thisListener = this; // refers to View.OnClickListener's object
                DontMissTwice thisDontMissTwice = DontMissTwice.this // refers to DontMissTwice's object
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文