gpt4 book ai didi

java - 如何在 setOnDragListener 中使用两个按钮?

转载 作者:行者123 更新时间:2023-12-02 05:38:03 27 4
gpt4 key购买 nike

我是 Android Studio 的新手,我了解一些有关它和 java 的基础知识。我不是专家。我正在做一个拖放游戏,我想拖放两个项目,代码正在工作,但我需要检查按钮是否位于特定的线性布局上,但两个按钮都经过验证。

这是针对 Android Studio 的

package com.example.lalo.menuhamburguesa;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.ClipData;

import android.view.DragEvent;
import android.view.MotionEvent;



public class Fragment3 extends Fragment {
Button dragbutton;
Button dragbutton1;
LinearLayout dropbutton;
LinearLayout dropbutton1;
TextView textView, sucess;
TextView textView1, sucess1;
int total, fail = 0;
int total1, fail1 = 0;

@Override
/*public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment3, container, false);
}*/

/*View v = inflater.inflate(R.layout.fragment_fragment2, container, false);
pdfViewer = (PDFView) v.findViewById(R.id.pdfView);
pdfViewer.fromAsset("Ejercicios.pdf").pages(0).load();*/


public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
setContentView(R.layout.activity_main);

With this:

return new SampleView(R.layout.activity_main);
*/
View v = inflater.inflate(R.layout.fragment_fragment3, container, false);


dragbutton = (Button) v.findViewById(R.id.one);
dropbutton = (LinearLayout) v.findViewById(R.id.drag_it_linear);

dragbutton1 = (Button) v.findViewById(R.id.one1);
dropbutton1 = (LinearLayout) v.findViewById(R.id.drag_it_linear1);


textView = (TextView) v.findViewById(R.id.Total);
sucess = (TextView) v.findViewById(R.id.Success);

textView1 = (TextView) v.findViewById(R.id.Total1);
sucess1 = (TextView) v.findViewById(R.id.Success1);


//================================================LISTENER========================


dropbutton.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
// Executed after startDrag() is called.
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
// Executed after the Drag Shadow enters the drop area
break;
case DragEvent.ACTION_DROP: {
//Executed when user drops the data
fail = fail + 1;
return (true);
}
case DragEvent.ACTION_DRAG_ENDED: {
total = total + 1;
int value = total - fail;
sucess.setText("Sucessful Drops:" + value);
textView.setText("Total Drops: " + total);
return (true);
}
default:
break;
}
return true;
}
});
dragbutton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(dragbutton);
v.startDrag(data, shadow, null, 0);
return false;
}
});



//================================================LISTENER========================

//================================================LISTENER========================


dropbutton1.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
final int action = event.getAction();

//CASE


switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
// Executed after startDrag() is called.
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
// Executed after the Drag Shadow enters the drop area
break;
case DragEvent.ACTION_DROP: {
//Executed when user drops the data
fail1 = fail1 + 1;
return (true);
}
case DragEvent.ACTION_DRAG_ENDED: {
total1 = total1 + 1;
int value1 = total1 - fail1;
sucess1.setText("Sucessful Drops:" + value1);
textView1.setText("Total Drops: " + total1);
return (true);
}
default:
break;
}

//CASE

return true;
}
});
dragbutton1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(dragbutton1);
v.startDrag(data, shadow, null, 0);
return false;
}
});



//================================================LISTENER========================


return v;
}
}

我希望每个 setOnDragListener 只进行一次验证,但我得到了两次

最佳答案

尽管您的拖动按钮消耗了 onTouch 操作,但它们都返回 false。看看如果让它们返回 true 会发生什么。

关于java - 如何在 setOnDragListener 中使用两个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56154797/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com