[英]Android custom Listview dialog
我已經設法基於這個Stackoverflow問題的答案創建了一個自定義列表視圖對話框。 如何在Android的警報對話框中顯示列表視圖
這是我的對話框自定義列表視圖的圖像。
我有一個問題,如果用戶分別選擇“編輯帖子” /“刪除帖子”,我該如何檢索用戶輸入?是否有用於對話框的OnClickListener以及如何實現它?謝謝:)
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
}
});
我認為您不必在對話框中將對話框視圖定義為列表視圖。 只需創建一個簡單的布局文件,如下所示:
<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()
方法中綁定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(...);
}
}
希望這會有所幫助。
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
}
});
}
});
您可以始終使用上下文菜單執行相同的操作。
首先在您的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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.