سؤال

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.hello_world, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.new_task:
    newTask(null);
    return true;
    default:
    return super.onOptionsItemSelected(item);
    }
}

public void newTask (View view) {whatever}

=====

How do I call newTask from the menu? Eclipse offered to add null argument.

هل كانت مفيدة؟

المحلول

If your newTask method is designed to be called by an onClick handler from a layout then you can add a tag to the view and then call view.getTag() inside your method.

That is one way that you could have many views call the same onclick handler method but then do different things.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top