我正在开发一个todo应用。在此应用程序中,todo项目作为列表项添加到列表视图中。每个列表项(对应于每个待办事项)具有多个视图元素的相对布局。我已为此列表视图注册了上下文菜单。但问题是,当我长按列表项时,它不会被触发。以下是代码块。请帮忙..
列表活动(显示待办事项的列表视图):
公共类ToDoManagerActivity扩展了ListActivity {
private static final int ADD_TODO_ITEM_REQUEST = 0;
private static final String FILE_NAME = "myToDos.txt";
private static final String TAG = "Lab-UserInterface";
// IDs for menu items
private static final int MENU_DELETE = Menu.FIRST;
private static final int MENU_DUMP = Menu.FIRST + 1;
private static final int MENU_ADD = Menu.FIRST + 2;
ToDoListAdapter mAdapter;
TextView footerView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// TODO - Attach the adapter to this ListActivity's ListView
getListView().setAdapter(mAdapter);
// register for context menu
registerForContextMenu(getCurrentFocus());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater contextMenu = getMenuInflater();
contextMenu.inflate(R.menu.menu_context, menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MENU_DELETE, Menu.NONE, "Delete all");
menu.add(Menu.NONE, MENU_DUMP, Menu.NONE, "Dump to log");
menu.add(Menu.NONE, MENU_ADD, Menu.NONE, "Add ToDo");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_DELETE:
mAdapter.clear();
return true;
case MENU_DUMP:
dump();
return true;
case MENU_ADD:
Intent mAddToDo = new Intent(ToDoManagerActivity.this,
AddToDoActivity.class);
startActivityForResult(mAddToDo, ADD_TODO_ITEM_REQUEST);
return true;
default:
return super.onOptionsItemSelected(item);
}
}}
列表项的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/titleView"
android:textColor="#000000"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
<TextView
android:id="@+id/StatusLabel"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/statusCheckBox"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="17dp"
android:text="@string/done_string" >
</TextView>
<CheckBox
android:id="@+id/statusCheckBox"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="52dp"
android:layout_toRightOf="@+id/StatusLabel" >
</CheckBox>
<TextView
android:id="@+id/PriorityLabel"
android:textColor="#000000"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/statusCheckBox"
android:layout_alignTop="@+id/StatusLabel"
android:layout_toLeftOf="@+id/priorityView"
android:text="@string/priority_string" >
</TextView>
<TextView
android:id="@+id/priorityView"
android:textColor="#000000"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/statusCheckBox"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/StatusLabel" >
</TextView>
<TextView
android:id="@+id/DateLabel"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/statusCheckBox"
android:text="@string/date_string" >
</TextView>
<TextView
android:id="@+id/dateView"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/DateLabel"
android:layout_toRightOf="@+id/DateLabel" >
</TextView>
</RelativeLayout>
上下文菜单xml如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/rajit_me"
android:title="Rajit"/>
<item
android:id="@+id/mou_me"
android:title="Mou"/>
<item
android:id="@+id/suha_me"
android:title="Suha"/>
<item
android:id="@+id/samah_me"
android:title="samah"/>
</menu>
答案 0 :(得分:1)
我已经解决了这个问题:只需将以下两个属性添加到列表项的清单视图中:
<CheckBox
android:id="@+id/statusCheckBox"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="52dp"
android:layout_toRightOf="@+id/StatusLabel"
<!-- add these two attributes to disable the focus of the check box -->
android:focusable="false"
android:focusableInTouchMode="false"
>
</CheckBox>