我是Android Studio的新手,我了解一些有关Java和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仅进行一次验证,但是我得到两次
答案 0 :(得分:0)
尽管两个拖动按钮都使用了onTouch操作,但它们均返回false。看看如果使它们返回true,会发生什么情况。