android 2.0.3中的onlongclicklistener

时间:2012-02-16 05:29:33

标签: android android-alertdialog onlongclicklistener

在我的java代码中,我使用onLongClickListener并打开1 AlerDailogBox,显示删除或不显示!!但我想在我的longclick上显示一些选项,并根据我想要进一步做的选择。我不想使用上下文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);
                 } 

1 个答案:

答案 0 :(得分:0)

从您的问题中发出声音,我认为您是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