對於按鍵長按所有你需要做的就是這個
down.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
然後創建你需要創建一個菜單資源的上下文菜單。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game" />
<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="@string/help" />
</menu>
然後在你的活動覆蓋此方法並膨脹上下文菜單
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
這是你如何捕捉選擇哪個項目,並採取相應的行動,以它
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
現在你do是在長按這樣的註冊按鈕的上下文菜單像這樣..
registerForContextMenu(down); //Your button
編輯:要顯示一個日期選擇器只需創建一個dialogDate選擇器,並在上下文菜單中的開關語句添加
showDialog(DATE_DIALOG_ID);
..
這裏是你如何創建一個日期選擇器對話框
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
static final int DATE_DIALOG_ID = 0;
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
Thanks @ coder_For_Life22。當你選擇其中一個上下文項目時,你可以顯示一些框架代碼來說明如何調用日期選擇器? – aez 2012-01-07 16:50:40
當然可以。查看我的編輯。 – 2012-01-07 18:57:39
太好了,謝謝。我不確定這是如何工作的,但如果你認爲我的問題很好,請投票支持。 – aez 2012-01-07 19:20:30