如何在片段中显示对话| Android的

时间:2014-04-16 14:47:52

标签: android android-fragments

在我的活动中使用fragment,我想在dialogue ItemLongClickListener上显示viewlist。我尝试了一些代码但失败了。反正有吗?

code

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
     public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
                    Toast.makeText(getActivity(),"thul thuk!",Toast.LENGTH_SHORT).show();  
              //show-dialogue here
           return false;
                }
    });

2 个答案:

答案 0 :(得分:1)

试试这个..

<强> custom_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFFFFF" />
     </LinearLayout>

<强>爪哇

 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
 public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
                Toast.makeText(getActivity(),"thul thuk!",Toast.LENGTH_SHORT).show();  
          Dialog dialog = new Dialog(getActivity());
           dialog.setContentView(R.layout.custom_dialog);
           dialog.setTitle("Custom Dialog");
           dialog.show();  
       return false;
            }
});

修改

您可以使用 Context Menu

答案 1 :(得分:0)

试试这个:

public void showPopup(View anchorView) {

        View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);

        popupWindow = new PopupWindow(popupView, 
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


        Button proceed = (Button)popupView.findViewById(R.id.button1);
        Button cancel = (Button)popupView.findViewById(R.id.button2);

        cb = (CheckBox)popupView.findViewById(R.id.checkBox1); 

        proceed.setOnClickListener(onProceed); 
        cancel.setOnClickListener(onCan); 

        popupWindow.setFocusable(true);


        popupWindow.setBackgroundDrawable(new ColorDrawable());

        int location[] = new int[2];


        anchorView.getLocationOnScreen(location);

        popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, 
                location[0], location[1] + anchorView.getHeight());

    }

上面会弹出一个弹出窗口,就在点击的视图下方。

popup_layout是将被夸大的XML布局文件。