Launching Activities from a View
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 tes开发者_如何学CtButton2;
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.
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.
精彩评论