简体   繁体   中英

Menu Item on a button click

I am trying to get an image on a menu item. But in the tutorial it is using an alphabet to view the menu item. I want to view a button on the click on a button. Currently the code is not working. I am referring to a tutorial. Please guide me to the best way to do it.

My code:

package com.example.androiddemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

public class nextpagejava extends Activity implements OnClickListener{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextpage);
    Button bt1 = (Button) findViewById(R.id.btn1);
    bt1.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
  }
  public void CreateMenu(Menu m){
    m.setQwertyMode(true);
    MenuItem mItm1 = m.add(0, 0, 0, "Item1");
    {
      mItm1.setAlphabeticShortcut('a');
      mItm1.setIcon(R.drawable.images);
    }
  }
  private boolean MenuChoice(MenuItem item){
    switch(item.getItemId()){
      case 0:
        Toast.makeText(this, "You clicked on item 1", Toast.LENGTH_LONG).show();
        return true;
      }
      return false;
    }
  }

如果您询问使用图标来使ActionBar溢出,则不能这样做-只有文本描述可用于此操作。

Hope you are looking for Adding Image to Menu's. If I understand correct, the below tutorial helps you to do that,

http://tonysilvestri.com/blog/2010/08/25/android-options-menu-with-an-image/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM