【问题标题】:onlongclick listner in android 2.0.3android 2.0.3 中的 onlongclick 监听器
【发布时间】:2012-02-16 05:29:33
【问题描述】:

在我的 java 代码中,我使用了 onLongClickListener 并打开了 1 个 AlerDailogBox,显示是否删除!!但我想在我的 longclick 上显示一些选项,并根据我想做的选择做进一步的事情。我不想使用 context menu.plz 建议我在当前代码中应该进行哪些更改?

 OnLongClickListener myListener = new OnLongClickListener() {
                         public boolean onLongClick(final View v) {

                         // do something on long click
                         AlertDialog alertDialog = new AlertDialog.Builder(v.getContext()).create();
                         alertDialog.setTitle("Do you want to Delete?");
                         alertDialog.setMessage(" "+temp_name);
                         alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                EstimateTrackerActivity.this.dh.deleteexp(inc_id);
                                /*//Toast.makeText(EstimateTrackerActivity.this, "id"+id,Toast.LENGTH_LONG).show();
                                onclick_addcategory(v);*/
                                onclick_listexpense(v);
                                spinner.setSelection(temp3);
                            }
                         });
                         alertDialog.setButton2("CANCEL", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                onclick_listexpense(v);
                                spinner.setSelection(temp3);
                            }
                         });
                         alertDialog.show();
                       return false;
                     }
                 };  tr_inc.setOnLongClickListener(myListener);
                 } 

【问题讨论】:

    标签: android android-alertdialog onlongclicklistener


    【解决方案1】:

    从你的问题看来,我认为你是 Android 新手。

    好的,看看下面的代码。

    要创建一个带有一系列可选项目的AlertDialog,如右图所示,请使用setItems() 方法:

    final CharSequence[] items = {"Red", "Green", "Blue"};
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick a color");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alert = builder.create();
    

    更多信息请查看Creating an AlertDialog

    【讨论】:

    • 是的,我是 android 新手。很抱歉提出这个基本问题。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 2011-10-01
    • 1970-01-01
    • 2016-05-29
    • 2011-03-31
    相关资源
    最近更新 更多