从视图启动活动

发布于 2024-12-08 06:15:36 字数 2363 浏览 0 评论 0原文

我想知道以模块化方式从其他视图启动活动的最佳方法是什么。我试图找出一种方法来告诉我的“按钮”在“onTouchEvent”中选择后要触发哪个活动。目前,我有一个主要活动,它创建我的视图并将其设置为我的“MainMenu”。我的主菜单定义了一个 MenuItem 类,该类定义了一个用于绘制按钮的矩形,并在相交/触摸/单击时触发活动。但是,我在启动该活动时遇到一些困难。下面只是一些代码片段,展示了我想要实现的一些目标:

public class MainMenu extends View {
...
private Vector<MenuItem> menuItems;
private MenuItem testButton;
private MenuItem testButton2;

public MainMenu(Context context) {
    ...
    // Create our menu buttons and load their specific images
    testButton = new MenuItem(context, new OptionsMenu(), 150, 50, imgButtons, 256, 64, 0, 0);
    testButton2 = new MenuItem(context, OptionsMenu.class, 150, 200, imgButtons, 256, 64, 0, 0);

    // Store our buttons
    menuItems = new Vector<MenuItem>(5, 2);
    menuItems.add(testButton);
    menuItems.add(testButton2);
}

...

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN)
        super.onTouchEvent(event);

    // Create our menu item iterator 
    Iterator<MenuItem> menuItemsIter = menuItems.iterator();
    Object element;

    // Loop through our menu items, drawing them
    while(menuItemsIter.hasNext()) {
        element = menuItemsIter.next();
        if(((MenuItem)element).HasIntersected((int)event.getX(), (int)event.getY())) {
            ((MenuItem)element).LaunchActivity();
        }
    }

    return true;
}
}

class MenuItem  {
...
private Context container = null;   // Indicates which activity contains us
private Object startObject = null;  // Which activity we'll start/execute

public MenuItem(Context context, Object object, int xPos, int yPos, Bitmap image, 
                int imageWidth, int imageHeight, int xOffset, int yOffset) {
    ...
    container = context;
    startObject = object;
}

... 

public void LaunchActivity() {
    if(startObject != null) {
        Intent activity = new Intent(container, startObject.getClass());
        container.startActivity(activity);
    }
}
}

我尝试以两种不同的方式设置 MenuItem 的对象(new OptionsMenu() 和 OptionsMenu.class),但似乎都不起作用。我尝试在创建新 Intent 时避免使用 MenuItem 的 startObject,并使用 (container, optionsMenu.class) 作为参数。这也不起作用。据我所知,这是启动活动的正确方法,但我想我在某个地方错过了一个步骤。

另外,我读了一些人们提到回调的使用的文章/帖子,但是是在活动端而不是视图端。然而,目前还不清楚我是否应该使用内置的 Android 回调,或者是否应该创建自己的回调并设置自己的系统。

任何有关我做错了什么,或者我可以采取哪些不同措施来以不同/更好的方式处理此问题的信息将不胜感激。谢谢。

I was wondering what the best way to approach launching activities from other views in a modular way. I'm trying to figure out a way to tell my "button" which activity to fire off once its been selected in the 'onTouchEvent'. Currently, I have a main activity that creates and sets my view to my 'MainMenu'. My main menu defines a MenuItem class that defines a rect for drawing a button, and firing off an activity when intersected/touched/clicked. However, I'm having some difficulty firing off the activity. Below are just a few snippets of code demonstrating some of what I'm trying to achieve:

public class MainMenu extends View {
...
private Vector<MenuItem> menuItems;
private MenuItem testButton;
private MenuItem testButton2;

public MainMenu(Context context) {
    ...
    // Create our menu buttons and load their specific images
    testButton = new MenuItem(context, new OptionsMenu(), 150, 50, imgButtons, 256, 64, 0, 0);
    testButton2 = new MenuItem(context, OptionsMenu.class, 150, 200, imgButtons, 256, 64, 0, 0);

    // Store our buttons
    menuItems = new Vector<MenuItem>(5, 2);
    menuItems.add(testButton);
    menuItems.add(testButton2);
}

...

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN)
        super.onTouchEvent(event);

    // Create our menu item iterator 
    Iterator<MenuItem> menuItemsIter = menuItems.iterator();
    Object element;

    // Loop through our menu items, drawing them
    while(menuItemsIter.hasNext()) {
        element = menuItemsIter.next();
        if(((MenuItem)element).HasIntersected((int)event.getX(), (int)event.getY())) {
            ((MenuItem)element).LaunchActivity();
        }
    }

    return true;
}
}

class MenuItem  {
...
private Context container = null;   // Indicates which activity contains us
private Object startObject = null;  // Which activity we'll start/execute

public MenuItem(Context context, Object object, int xPos, int yPos, Bitmap image, 
                int imageWidth, int imageHeight, int xOffset, int yOffset) {
    ...
    container = context;
    startObject = object;
}

... 

public void LaunchActivity() {
    if(startObject != null) {
        Intent activity = new Intent(container, startObject.getClass());
        container.startActivity(activity);
    }
}
}

I tried setting my MenuItem's Object two different ways (new OptionsMenu() and OptionsMenu.class), but neither seem to work. I tried dodging the use of the MenuItem's startObject when created a new Intent, and using (container, optionsMenu.class) for parameters instead. Which didn't work either. From what I know this is the correct way to fire off an activity, but I'm guess I'm missing a step somewhere.

Also, I read a few articles/posts of people mentioning the use of callbacks, but on the Activity side instead of the View side. However, it wasn't very clear if there were built in Android callbacks I should use, or if I should create my own callbacks and setup my own system.

Any information about what I'm doing wrong, or what I could do differently to approach this differently/better would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爱本泡沫多脆弱 2024-12-15 06:15:36

恕我直言,只是一个 Button 支持将点击事件发送到 OnClickListener,您的自定义 View 应该有自己的自定义事件接口来发送自己的自定义事件到控制器(例如,一个活动)。由控制器来安排对这些事件执行某些操作,例如启动其他活动。

IMHO, just a Button supports sending click events to an OnClickListener, your custom View should have its own custom event interface for sending its own custom events to the controller (e.g., an activity). It is up to the controller to arrange to do something with those events, such as starting up other activities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文