我已设法根据此Stackoverflow问题的答案创建自定义列表视图对话框。how-to-display-list-view-in-alert-dialog-in-android
这是我的对话框自定义列表视图的图像。
如果用户选择"编辑帖子"我有一个如何检索用户输入的问题? /"删除帖子"对于Dialog是否有一个OnClickListener,我该如何实现呢?谢谢:)
答案 0 :(得分:0)
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do things that use the position or
// the view content to understand user input
}
});
答案 1 :(得分:0)
我认为你不必在对话框中将对话框视图定义为listview。 只需创建一个简单的布局文件,如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="100dp"
android:layout_gravity="right">
<TextView
android:id="@+id/edit"
android:layout_width="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="120dp"
android:paddingLeft="10dp"
android:gravity="center"
android:clickable="true"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:background="@drawable/selector_option_menu"
android:text="EditPost"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/white"
/>
<TextView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="120dp"
android:paddingLeft="10dp"
android:gravity="center"
android:clickable="true"
android:textColor="@color/white"
android:background="@drawable/selector_option_menu"
android:layout_height="wrap_content"
android:text="Delete Post"/>
</LinearLayout>
创建一个自定义对话框类,扩展Dialog类,如下所示,并在onCreate()
mehtod中绑定onclick监听器:
public class MyDialog extends Dialog {
Context context;
public MyDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
public MyDialog(Context context, int theme){
super(context, theme);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.dialog);
findViewById(R.id.edit).setOnClickListener(...);
findViewById(R.id.delte).setOnClickListener(...);
}
}
希望这会有所帮助。
答案 2 :(得分:0)
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
builder.setSingleChoiceItems(items,0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
// TODO Auto-generated method stub
deger= which;
}
})
// Set the action buttons
.setPositiveButton("Editpost", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on
}
})
.setNegativeButton("delet post", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on
}
});
}
});
答案 3 :(得分:0)
你总是可以使用同样的事情的上下文菜单。
首先在ListView上注册一个监听器:
registerForContextMenu(myListView);
设置上下文菜单选项:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v==myLIstView)
{
menu.setHeaderTitle("Optional title");
menu.add(0, v.getId(), 0,"Edit post");
menu.add(0, v.getId(), 0,"Delete post");
}
}
捕获并对结果做一些事情:
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
long num= info.id;
if(item.getTitle()=="Edit post")
{
edit a post
}
else if(item.getTitle()=="Delete post"){
delete a post
}
}
return super.onContextItemSelected(item);
}