【问题标题】:What can I use to track which button was pressed?我可以使用什么来跟踪按下了哪个按钮?
【发布时间】:2011-06-06 19:24:10
【问题描述】:

我有一个按钮可以进入游戏模式。所有按钮都启动相同的活动,但规则和物理会根据您触摸的内容而改变。

我想跟踪按下的按钮,以便了解人们选择了经典模式还是训练模式,并相应地设置规则。我该怎么做?

这是我从菜单中启动游戏模式的方式:

MenuElement classic = mElements.get(0);
    MenuElement training = mElements.get(1);

        if(touchX > classic.mX  && touchX < classic.mX + classic.mBitmap.getWidth() 
           && touchY > classic.mY   && touchY < classic.mY + classic.mBitmap.getHeight())
        {
            aux = "Starting game";
            Context context = com.Juggle2.Menu.this.getContext(); 
            Intent intent = new Intent(context, StartGame.class);
            intent.putExtra("rule", 1);
            context.startActivity(intent);
        }

        if(touchX > training.mX  && touchX < training.mX + classic.mBitmap.getWidth() 
           && touchY > training.mY   && touchY < training.mY + training.mBitmap.getHeight())
        {
            aux = "Starting training";
            Context context = com.Juggle2.Menu.this.getContext(); 
            Intent intent = new Intent(context, StartGame.class);
            intent.putExtra("rule", 2);
            context.startActivity(intent);
        }

这就是它的去向:

public class StartGame extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new Panel (this));      //Start the game

    }
}

现在我需要我的 Panel 类(即 SurfaceView)可以访问该附加组件。


哦,我明白了:

我找到了一种方法,可以用更少的步骤完成我想做的事情

public class Global{
    public static int rules = 0;
}

现在我可以随时随地通过键入Global.rules 访问这些规则

事后看来,这似乎很简单。

【问题讨论】:

    标签: java android variables


    【解决方案1】:

    您可以向您的Intents 添加一个“额外的”来启动您的活动。

    Intent intent = new Intent(context, StartGame.class);
    intent.putExtra(String key, X value);
    startActivity(intent);
    

    然后,您可以通过以下方式在 StartGame Activity 中获得这些额外内容:

    Bundle extras = getIntent().getExtras();
    X value = extras.getX(String key);
    

    其中 X 是某种类型的数据(例如,String、int、long、double 等)。

    更新

    抱歉,我没有仔细阅读您的评论。下面是一些可能对你有用的东西:

    public class StartGame extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    
        Bundle extras = getIntent().getExtras();
        Panel mPanel = new Panel(this, extras);
    
        setContentView(mPanel);      //Start the game
        }
    }
    

    并更改 Panel 构造函数以接受 Bundle 参数,以便在初始化时获得这些值。让我知道它是如何工作的。

    【讨论】:

    • 谢谢,但我的 Activity 只是启动了 SurfaceView,这正是我需要这些变量的地方。我怎样才能将这些变量进一步传递一个类?
    • 再次感谢您,但我不会进一步传递它一个活动。该活动只是将内容视图设置为我的 SurfaceView。我将发布我的活动以向您展示我在说什么。 编辑好的,我用更多信息编辑了我的问题。我希望我的 Panel 类最终可以访问这些附加功能
    • 编辑编辑。编辑:或者,由于您只有一个“规则”额外,您可以将其作为额外参数传递。
    • 应该可以,但我找到了一种更简单的方法来做我想做的事。我将编辑我自己的问题以显示我做了什么
    【解决方案2】:

    您应该使用完成所有这些操作的库。否则你将不得不 1) 收集统计数据 2)将它们上传到您可以查看结果的地方,这部分可能很难自己完成

    但谷歌分析已经为您完成了这一切。也许其他人也... 这是starter page

    问候, 斯蒂芬

    【讨论】:

    • 我不想为度量目的跟踪按下的按钮,我想跟踪按下的按钮,以便知道人们选择了Classic 模式还是Training 模式,并设置相应地规则
    【解决方案3】:

    我找到了一种方法,可以用更少的步骤完成我想做的事情

    public class Global{
        public static int rules = 0;
    }
    

    现在我可以随时随地通过输入Global.rules访问这些规则

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 2011-10-30
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多