用到选项菜单和上下文菜单
public boolean onPrepareOptionsMenu(Menu menu)用来初始化选项菜单
public boolean onCreateOptionsMenu(Menu menu) 用于创建菜单
public boolean onOptionsItemSelected(MenuItem item)用于选择时的处理
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)创建上下文菜单,需要在onCreate()时注册:registerForContextMenu
public boolean onContextItemSelected(MenuItem item)用于上下文菜单选择时的处理
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
class ToDoItem
{
public String getTask()
{
return task;
}
String task;
public Date getCreated()
{
return created;
}
Date created;
public ToDoItem(String _task)
{
this(_task, new Date(java.lang.System.currentTimeMillis()));
}
public ToDoItem(String _task, Date _created)
{
task = _task;
created = _created;
}
public String toString()
{
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
return "(" + sdf.format(created) + ")" + task;
}
}
class ToDoItemAdapter extends ArrayAdapter
{
int resource;
public ToDoItemAdapter(Context _context, int _resource, List _items)
{
super(_context, _resource, _items);
resource = _resource;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout todoView;
ToDoItem item = getItem(position);
String taskString = item.getTask();
Date createDate = item.getCreated();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
String dateString = sdf.format(createDate);
if (convertView == null) {
todoView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, todoView, true);
} else {
todoView = (LinearLayout)convertView;
}
TextView dateView = (TextView)todoView.findViewById(R.id.rowDate);
TextView taskView = (TextView)todoView.findViewById(R.id.row);
dateView.setText(dateString);
dateView.setText(taskString);
return todoView;
}
}
class ToDoListItemView extends TextView
{
private Paint marginPaint;
private Paint linePaint;
private int paperColor;
private float margin;
public ToDoListItemView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
public ToDoListItemView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
private void init()
{
marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
marginPaint.setColor(Color.RED);
linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
linePaint.setColor(Color.BLACK);
paperColor = Color.BLUE;
margin = Color.CYAN;
}
public void onDraw(Canvas canvas)
{
canvas.drawColor(paperColor);
canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint);
canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint);
canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
canvas.save();
canvas.translate(margin, 0);
super.onDraw(canvas);
canvas.restore();
}
}
public class MainActivity extends Activity {
private ArrayList todoItems;
private ListView myListView;
private EditText myEditText;
private ToDoItemAdapter aa;
private boolean addingNew = false;
static final private int ADD_NEW_TODO = Menu.FIRST;
static final private int REMOVE_TODO = Menu.FIRST + 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (ListView)findViewById(R.id.myListView);
myEditText = (EditText)findViewById(R.id.myEditText);
todoItems = new ArrayList();
aa = new ToDoItemAdapter(getApplicationContext(), R.layout.row, todoItems);
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
ToDoItem newItem = new ToDoItem(myEditText.getText().toString());
todoItems.add(0, newItem);
myEditText.setText("");
aa.notifyDataSetChanged();
cancelAdd();
return true;
}
}
return false;
}
});
registerForContextMenu(myListView);
}
private void cancelAdd()
{
addingNew = false;
myEditText.setVisibility(View.GONE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu);
MenuItem itemAdd = menu.add(0, ADD_NEW_TODO, Menu.NONE, "New");
MenuItem itemRem = menu.add(0, REMOVE_TODO, Menu.NONE, "Remove");
itemAdd.setIcon(R.drawable.ic_launcher);
itemRem.setIcon(R.drawable.ic_launcher);
itemAdd.setShortcut('0', 'a');
itemRem.setShortcut('1', 'r');
return true;
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Selected To Do Item");
menu.add(0, REMOVE_TODO, Menu.NONE, "Remove");
}
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
int idx = myListView.getSelectedItemPosition();
String removeTitle = "Title";
MenuItem removeItem = menu.findItem(REMOVE_TODO);
removeItem.setTitle(removeTitle);
removeItem.setVisible(addingNew || idx > -1);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
int index = myListView.getSelectedItemPosition();
switch (item.getItemId()) {
case REMOVE_TODO:
if (addingNew) {
cancelAdd();
} else {
removeItem(index);
}
return true;
case ADD_NEW_TODO:
addNewItem();
return true;
}
return false;
}
public boolean onContextItemSelected(MenuItem item)
{
super.onContextItemSelected(item);
switch(item.getItemId()) {
case REMOVE_TODO:
AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int index = menuInfo.position;
removeItem(index);
return true;
}
return false;
}
private void addNewItem()
{
addingNew = true;
myEditText.setVisibility(View.VISIBLE);
myEditText.requestFocus();
}
private void removeItem(int _index)
{
todoItems.remove(_index);
aa.notifyDataSetChanged();
}
}
ArrayAdapter用到的xml: