سؤال

Main Activity

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

public void myClickHandler(View v) 
{ 
    switch(v.getId()) 
    { 
         case R.id.resetscoreboard:
             scoreboardreset();
             topText.setText("Scoreboard Has Been Reset!!");

    }
}

menumenu.xml

<item
    android:id="@+id/resetscoreboard"
    android:title="Reset Scoreboard"
    android:orderInCategory="1"
    android:onClick="myClickHandler">

</item>

Could you tell me why it does not do anything when I pull up the menu and click on it?

Thanks a lot, experts @ stack overflow!!

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

المحلول

Instead of defining click Handler, you need to implement:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {

    case R.id.resetscoreboard:
       scoreboardreset();
       topText.setText("Scoreboard Has Been Reset!!");
       break;

    default:
        break;
    }

    return true;
}

Go through this example to get exact idea for the implementation of Menu in Android.

نصائح أخرى

You should not use android:onClick attribute here.

Instead, override the method onOptionsItemSelected.

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