将 onlongclick 侦听器添加到警报对话框

Posted

技术标签:

【中文标题】将 onlongclick 侦听器添加到警报对话框【英文标题】:Add onlongclick listener to an alertdialog 【发布时间】:2012-02-27 01:45:11 【问题描述】:

我在 android 中有一个 AlertDialog,其中包含来自 sqlite 的好友列表。当我单击列表中的好友名称时,会调用该好友。我想要做的是将一个 longclicklistener 添加到列表中,以便提示我删除列表中的好友。我无法让 onlclick 和 onlongclick 在同一个元素上工作。有人可以在这里给我一个指针。我已经使用 android 工作了几个月。感谢您的帮助!

private void displayBuddyList(String region) 
        final String region2 = region;
        Context context = getApplicationContext();
        dh = new DataBaseHelper(context);

        List<String> bnames = dh.selectBuddies(); 
        Log.d(TAG, "Buddy Names: " +bnames);



    final CharSequence[] buds = bnames.toArray(new CharSequence[bnames.size()]);
//  final CharSequence[] items = "Mark", "Vikrant", "Olle,"Jane","Dan";

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select a Buddy");   
    builder.setItems(buds, new DialogInterface.OnClickListener() 



        public void onClick(DialogInterface dialogInterface, int item) 

        //  showShortToast("Clicked on:"+buddy[item]);
            String ptcode =  buds[item].toString();;




        if (region2 == "A")  

                callbuddy(ptcode,region2);

             else if  (region2 == "E") 

                        callbuddy(ptcode,region2);


            else if  (region2 == "P") 

                        callbuddy(ptcode,region2);



             else 
                 showShortToast("We have a bug"); 
            

           return;
        
    );
    builder.create().show();

【问题讨论】:

【参考方案1】:

添加 OnLongClickListener 的一种方法是覆盖对话框的 OnShowListener 并在 onShow(DialogInterface dialog) 方法中设置 OnItemLongClickListener。试试这个:

private void displayBuddyList(String region) 
    final String region2 = region;
    Context context = getApplicationContext();
    dh = new DataBaseHelper(context);
    List<String> bnames = dh.selectBuddies(); 
    Log.d(TAG, "Buddy Names: " +bnames);

final CharSequence[] buds = bnames.toArray(new CharSequence[bnames.size()]);
//  final CharSequence[] items = "Mark", "Vikrant", "Olle,"Jane","Dan";

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Buddy");   
builder.setItems(buds, new DialogInterface.OnClickListener() 

    public void onClick(DialogInterface dialogInterface, int item) 
    //  showShortToast("Clicked on:"+buddy[item]);
        String ptcode =  buds[item].toString();;
    if (region2 == "A")  
            callbuddy(ptcode,region2);
         else if  (region2 == "E") 
                    callbuddy(ptcode,region2);
        else if  (region2 == "P") 
                    callbuddy(ptcode,region2);
         else 
             showShortToast("We have a bug"); 
        
       return;
    
);

final AlertDialog ad = builder.create(); //don't show dialog yet
ad.setOnShowListener(new OnShowListener() 
       
    @Override
public void onShow(DialogInterface dialog) 
       
        ListView lv = ad.getListView(); //this is a ListView with your "buds" in it
        lv.setOnItemLongClickListener(new OnItemLongClickListener() 
    
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 
    
        Log.d("Long Click!","List Item #"+position+"was long clicked");
        return true;
               
    );     

);
ad.show();

【讨论】:

以上是关于将 onlongclick 侦听器添加到警报对话框的主要内容,如果未能解决你的问题,请参考以下文章

无法从警报对话框中的 EditText 视图中获取数据

如何将第三个按钮添加到 Android 警报对话框?

onTouchEvent onClick onLongClick 调用

Android中按钮组的onLongClick监听器

Android数据绑定onLongClick不起作用

如何在按钮上同时实现DialogBox和onLongClick?