<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/copy"
android:orderInCategory="100"
android:title="@string/create_class"/>
<item
android:id="@+id/delete"
android:orderInCategory="100"
android:title="@string/create_exam"/>
</menu>
package com.example.phonemenu;
import android.os.Bundle;
import android.app.Activity;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_add);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
PopupMenu popup = new PopupMenu(MainActivity.this,v);// 第二个参数是绑定的那个view
// 获取菜单填充器
MenuInflater inflater = popup.getMenuInflater();
// 填充菜单
inflater.inflate(R.menu.main, popup.getMenu());
// 绑定菜单项的点击事件
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.copy:
Toast.makeText(MainActivity.this, "创建课堂···", Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
Toast.makeText(MainActivity.this, "创建考试···", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
});
popup.show();
}
});
}
}