How to use an enum in a static and non-static way

I have 2 activities, `ActivityA` and `ActivityB`, with 3 buttons each. I show `ActivityA` on one connected device and `ActivityB` on the other connected device. The buttons can each be in 1 of 2 states but the states need to be reflected on both devices. I'm using a util class with static methods to receive button presses from both activities and send it to the service as well as update the state of the buttons in both activities upon the callback from a service. Right now, each button calls a function in its corresponding activity from the xml. For example `Button1`'s `onClick` looks like this: android:onClick="button1Clicked" That function (from its corresponding activity) calls a function in UtilClass and looks like this: public void button1Clicked(View view) { UtilClass.button1Cliked(); } I set an `enum` in `UtilClass` so I know which button to update (in both `ActivityA` and `ActivityB`) upon a callback from the service. It looks like this: public class UtilClass { public static Button buttonPressed = WfdOperationButton.NONE; public enum Button { BUTTON1, BUTTON2, BUTTON3 } public static void button1Cliked() { buttonPressed = Button.BUTTON1; //call asynctask to tell the service button1 was pushed by passing //buttonPressed } public static void button2Cliked() { buttonPressed = Button.BUTTON2; //call asynctask to tell the service button2 was pushed by passing //buttonPressed } ... My question is how can I use the same `enum` from `UtilClass` in `ActivityA` and `ActivityB` to consolidate the 3 `buttonXClicked()` functions in `UtilClass`? My attempted change in `UtilClass` is below: public static void buttonCliked(Button inButton) { buttonPressed = inButton; //call asynctask to tell the service inButton was pushed by passing //buttonPressed } This doesn't work though because I can't use a non-static `enum` in `ActivityA` or `ActivityB` in a static function to pass in. But I need the enum to be non-static since I need to use it in the `UtilClass`. I'm new to Android so if I'm doing anything else wrong, I'd be grateful if you pointed it out. Thanks!

以上就是How to use an enum in a static and non-static way的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准