我需要在长按列表项时显示删除按钮..
我有长按的代码..但不知道如何编写代码以在此长按内显示按钮...
终于得到答案了...
.xml 文件
<ImageButton
android:id="@+id/imgdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/delete"
android:visibility="invisible"/>
.java 文件
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
arg1.findViewById(R.id.imgdelete).setVisibility(View.VISIBLE);
return false;
}
});
}
首先,您必须使用代码使删除按钮不可见,或者在 xml 文件中设置它的属性。当用户单击时,longpress
您必须使该删除按钮可见。删除操作完成后,再次使该按钮不可见。
您可以使用警报对话框。这是一个例子
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
final CharSequence[] items = { "Delete Item" };
AlertDialog.Builder builder = new AlertDialog.Builder(
[CLASS_NAME].this);
builder.setTitle("Delete Item");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Intent i;
switch (item) {
case 0:
AlertDialog.Builder builder = new AlertDialog.Builder(
SelectProfile.this);
builder.setMessage(
"Are you sure you want to delete?")
.setCancelable(false)
// Prevents user to use "back button"
.setPositiveButton(
"Delete",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
//Todo code here
}
})
.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});
builder.show();
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
return false;
}
});