在我的应用程序中,我有一个弹出式菜单,当我点击menuitem它收集gps位置时,问题是当我双击菜单触发两次,但我只做onclick 确定我在做错的地方Android:OnClick不能正常工作
LinearLayout popupmain = (LinearLayout) view
.findViewById(R.id.popupmain);
ImageView popupImage = (ImageView) view.findViewById(R.id.popupimg);
TextView popupName = (TextView) view.findViewById(R.id.popupname);
popupImage.setImageResource(popUpMenu.getImageResource());
popupName.setText(popUpMenu.getMenuName());
popupName.setTag(popUpMenu);
popupmain.setTag(popUpMenu);
popupName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doAction(v, activity);
}
});
popupmain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doAction(v, activity);
}
});
return view;
}
public void doAction(View v, Activity activity) {
Context myContext = v.getContext();
PopUpMenu popUpMenu = (PopUpMenu) v.getTag();
String result = popUpMenu.getMenuName();
if (result != null
&& result.equalsIgnoreCase(myContext.getResources().getString(
R.string.savecurrentlocation))) {
getLocation();
}
private void getLocation() {
manager = (LocationManager) parentActivity
.getSystemService(Context.LOCATION_SERVICE);
listener = new ManageLocation();
listener.setSucessCallBack(this, "setLocation");
listener.setFailureCallBack(this, "setNoLocation");
manager.requestLocationUpdates(
new Utils().getCurrentPlaceLocationProvider(manager), 0, 0,
listener);
}
任何帮助表示赞赏。
我popupmain XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popupmain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp" >
<ImageView
android:id="@+id/popupimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/popupname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:text="@string/default_date"
android:textColor="#000000"
android:textSize="15dp"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
把你的xml在你的问题请 – ibrahimyilmaz
不知道为什么你有两个'onClickListeners'执行相同的任务,其中只有一个在'popupmain'应该是足够的,但每一个他自己的,我想。无论如何,为什么不使用布尔标志并通过在'public void doAction()'的开始处设置_true_并在'private void getLocation()'的末尾将其设置为false来检查任务是否正在运行?在调用'doAction(v,activity);'之前检查'OnClickListener's'中的标志状态。我认为这应该做到这一点。 –
你可以用代码 – teekib