简体   繁体   English

布尔值的常量侦听器-Java Android

[英]Constant listener for Boolean - Java Android

In my android app, I'm trying to check whether a boolean has changed and if it has then do something. 在我的Android应用程序中,我正在尝试检查布尔值是否已更改,以及是否已经执行了某些操作。 But it's not constantly checking it. 但这并不是经常检查。 I believe I need a listener, but I'am unaware on how to structure it or where to put it. 我相信我需要一个听众,但是我不知道如何组织它或放在哪里。 I've looked online and can really only find onClick s etc, no boolean ones. 我在网上看过,实际上只能找到onClick等,没有布尔值。

Here is what I have so far: 这是我到目前为止的内容:

public boolean gameStartTouch = false;
public boolean titleVisible = true;
/*
public boolean isJumping = false;
public boolean isGrounded = false;
*/

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_main);    
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    int eventaction = event.getAction();

    switch (eventaction) {
    case MotionEvent.ACTION_DOWN:
    if (gameStartTouch == false) {

        gameStartTouch = true;
        titleVisible = false;
    }
    break;
    }
    return true;
}

and this is what needs to go in the listener: 这是侦听器中需要执行的操作:

ImageView titleView = (ImageView) findViewById(R.id.titleImg);
ImageView startNotifView = (ImageView) findViewById(R.id.startNotifImg);

if (titleVisible == false) {    
    titleView.setVisibility(View.INVISIBLE);
    startNotifView.setVisibility(View.INVISIBLE);
} else {
    titleView.setVisibility(View.VISIBLE);
    startNotifView.setVisibility(View.VISIBLE);
}

Thanks in advance. 提前致谢。 I couldn't find much info on it for these circumstances specifically. 在这些情况下,我找不到很多相关信息。

Do some modifying to your code, manage it like so 对您的代码进行一些修改,像这样进行管理

@Override
public boolean onTouchEvent(MotionEvent event) {
    int eventaction = event.getAction();

    switch (eventaction) {
    case MotionEvent.ACTION_DOWN:

        titleView.setVisibility(View.INVISIBLE);
        startNotifView.setVisibility(View.INVISIBLE);
        break;
    }
    return true;
}

I would put your findViewById() calls in onCreate to avoid unnecessary resource fetching. 我会将您的findViewById()调用放在onCreate上,以避免不必要的资源获取。 The only thing I see is that you are only actually switching your booleans once. 我唯一看到的是您实际上只交换了一次布尔值。 Is there any other time you plan to reset gameStartTouch to false and titleVisible to true after the first touch? 您是否还打算在第一次触摸后将gameStartTouch重置为false并将titleVisible重置为true?

@fire_head you can simply declare titleView and startNotifView as your class private members. @fire_head您可以简单地将titleViewstartNotifView声明为您的类私有成员。 Initialize them in onCreate method like this: 像这样在onCreate方法中初始化它们:

ImageView titleView = (ImageView) findViewById(R.id.titleImg); 
ImageView startNotifView = (ImageView) findViewById(R.id.starting);

and access them in handleGameStartTouch method. 并在handleGameStartTouch方法中访问它们。

Sorry, I had to post it as an answer as I couldn't comment on the answer above. 抱歉,我无法将其发布为答案,因为我无法评论以上答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM