在拖动监听多个IF语句 [英] On Drag Listener multiple IF statements

查看:131
本文介绍了在拖动监听多个IF语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个字母拖动ñ下降游戏(有点像猜牌),你有一个形象,你必须将信件拖到布局,形成了正确的答案。

在顶端,我有四个布局(layoutAnsw A至D),并在底部我有四个按钮 (btnTop A至D) 我有OnTouchListener和OnDragListener做工精细,除了一个单一的事情。

当我有多个类似的字符(字母),会发生什么? 例如,在本图片:

正如你所看到的,我得需要A字母要拖,我想不管你把第一次其中一个做到这一点。在我的code,我设法得到这样的:

如果第一个A是第一个空间,那么秒进入第二空间

我想code周围的其他方法包括previous声明。 我的code到目前为止让你把公司所有的A中的任何空间,包括2个字母在同一个空间。 pretty的没用。

我的code到目前为止

 公共类OneQuestionA扩展片段实现OnTouchListener,
    OnDragListener {

受保护的静态最后弦乐的logcat = NULL;
INT numDragged = 0;

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){
    // TODO自动生成方法存根
    查看rootView = inflater.inflate(R.layout.questions_fragment5,
            集装箱,假);

    btnTopA.setOnTouchListener(本); //字母A
    btnTopB.setOnTouchListener(本); //第二个字母A
    btnTopC.setOnTouchListener(本);
    btnTopD.setOnTouchListener(本);

    rootView.findViewById(R.id.layoutAnswA).setOnDragListener(本); //布局1
    rootView.findViewById(R.id.layoutAnswB).setOnDragListener(本); //布局2
    rootView.findViewById(R.id.layoutAnswC).setOnDragListener(本); //布局3
    rootView.findViewById(R.id.layoutAnswD).setOnDragListener(本); //布局4
返回rootView;
}
 

我的onTouch实现:

  @覆盖
公共布尔onTouch(查看视图,MotionEvent motionEvent){
    // TODO自动生成方法存根
    如果(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
        DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(视图);
        view.startDrag(空,shadowBuilder,视图,0);
        view.setVisibility(View.INVISIBLE);
        返回true;
    }

    如果(motionEvent.getAction()== MotionEvent.ACTION_UP){
        view.setVisibility(View.VISIBLE);
        返回true;
    } 其他 {
        返回false;
    }
}
 

ondrag当我执行:

  @覆盖
公共ondrag当布尔(视图V,的dragEvent E){

    INT行动= e.getAction();
    查看查看=(查看)e.getLocalState();

    开关(动作){
    案例DragEvent.ACTION_DRAG_STARTED:
        返回true;
    案例DragEvent.ACTION_DRAG_ENTERED:
        返回false;
    案例DragEvent.ACTION_DRAG_LOCATION:
        返回false;
    案例DragEvent.ACTION_DRAG_EXITED:
        返回false;
    案例DragEvent.ACTION_DROP:


        如果(view.getId()== R.id.btnTopA&安培;&安培; v.getId()== R.id.layoutAnswA){
            ViewGroup中所有者=(ViewGroup中)view.getParent();
            owner.removeView(视图);
            的LinearLayout容器=(LinearLayout中)V;
            container.addView(视图);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(空);
            view.setOnDragListener(空);
            numDragged ++;
        }否则,如果(view.getId()== R.id.btnTopB和放大器;&安培; v.getId()== R.id.layoutAnswB){
                ViewGroup中所有者=(ViewGroup中)view.getParent();
            owner.removeView(视图);
            的LinearLayout容器=(LinearLayout中)V;
            container.addView(视图);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(空);
            view.setOnDragListener(空);
            numDragged ++;
            }
        }





        如果(numDragged> = 4){
            numDragged = 0;

            Toast.makeText(getActivity(),
                    在正确的地方的所有按钮,Toast.LENGTH_SHORT)
                    。显示();


        }

    案例DragEvent.ACTION_DRAG_ENDED:
        如果(dropEventNotHandled(E)){
            view.setVisibility(View.VISIBLE);
        }
    }
    返回false;
}

私人布尔dropEventNotHandled(的dragEvent E){
    // TODO自动生成方法存根
    返回e.getResult()!;
}
 

解决方案

大量的阅读后,我遇到了非常简单的行:

  getChildCount()
 

使用,作为一个条件,随时有已经到位另一种观点认为,它只是返回false

  INT I = container.getChildCount();
如果(ⅰ&小于1){
// 干点什么
}否则如果(ⅰ== 1){
返回false;
}
 

I'm making a Letter Drag n Drop game (kinda like Guess the Brand) where you have an image and you have to drag the letters into layouts to form the correct answer.

At the top, I have Four layouts (layoutAnsw A to D), and in the bottom I have Four buttons (btnTop A to D) I have the OnTouchListener and OnDragListener working fine, except one single thing.

What happens when I have more than one similar character (letter)? For example in this image:

As you can see, I have to "A" letters that need to be dragged, and I want to do it regardless of which one you put first. In my code I managed to get something like this:

"If the First A is in the First space, then Second A goes to Second Space"

I'm trying to code the other way around including that previous statement. My code so far let's you put any "A" in any space, including 2 letters in the same space. Pretty useless.

My code so far

public class OneQuestionA extends Fragment implements OnTouchListener,
    OnDragListener {

protected static final String LOGCAT = null;
int numDragged = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.questions_fragment5,
            container, false);

    btnTopA.setOnTouchListener(this); // Letter A
    btnTopB.setOnTouchListener(this); // Second Letter A
    btnTopC.setOnTouchListener(this); 
    btnTopD.setOnTouchListener(this);

    rootView.findViewById(R.id.layoutAnswA).setOnDragListener(this); // Layout 1
    rootView.findViewById(R.id.layoutAnswB).setOnDragListener(this); // Layout 2
    rootView.findViewById(R.id.layoutAnswC).setOnDragListener(this); // Layout 3
    rootView.findViewById(R.id.layoutAnswD).setOnDragListener(this); // Layout 4
return rootView;
}

My onTouch implementation:

 @Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    // TODO Auto-generated method stub
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(null, shadowBuilder, view, 0);
        view.setVisibility(View.INVISIBLE);
        return true;
    }

    if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
        view.setVisibility(View.VISIBLE);
        return true;
    } else {
        return false;
    }
}

My onDrag implementation:

@Override
public boolean onDrag(View v, DragEvent e) {

    int action = e.getAction();
    View view = (View) e.getLocalState();

    switch (action) {
    case DragEvent.ACTION_DRAG_STARTED:
        return true;
    case DragEvent.ACTION_DRAG_ENTERED:
        return false;
    case DragEvent.ACTION_DRAG_LOCATION:
        return false;
    case DragEvent.ACTION_DRAG_EXITED:
        return false;
    case DragEvent.ACTION_DROP:


        if (view.getId() == R.id.btnTopA && v.getId() == R.id.layoutAnswA) {
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(null);
            view.setOnDragListener(null);
            numDragged++;
        } else if (view.getId() == R.id.btnTopB && v.getId() == R.id.layoutAnswB) {
                ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) v;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            view.setOnTouchListener(null);
            view.setOnDragListener(null);
            numDragged++;
            }
        } 





        if (numDragged >= 4) {
            numDragged = 0;

            Toast.makeText(getActivity(),
                    "All buttons in the Right place", Toast.LENGTH_SHORT)
                    .show();


        }

    case DragEvent.ACTION_DRAG_ENDED:
        if (dropEventNotHandled(e)) {
            view.setVisibility(View.VISIBLE);
        }
    }
    return false;
}

private boolean dropEventNotHandled(DragEvent e) {
    // TODO Auto-generated method stub
    return !e.getResult();
}

解决方案

After a lot of reading, I came across the incredibly simple line:

getChildCount() 

Use that as a condition, anytime there's already another view in place, it will just return false

int i = container.getChildCount();
if (i < 1) {
// Do Something
} else if (i == 1) {
return false;
}

这篇关于在拖动监听多个IF语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆