如何限制拖放次数

时间:2015-07-16 12:30:26

标签: android drag-and-drop

  1. 如何检索drop target的id(调用ChoiceDragListener()类的按钮)
  2. 如何指定拖放限制,即。可以完成“拖放”多少次
  3. 这是我的代码

      

    设计activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:background="@drawable/g"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
      <TableLayout 
         android:id="@+id/table"
         android:paddingTop="50dp"
        android:layout_width="wrap_content"    
               android:layout_height="wrap_content">
    
    
        <TableRow >
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/b1"          
               android:layout_margin="5dp"
              android:background="@android:color/transparent"
              android:textSize="26sp" 
    
                  android:text="a"/>
             <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/b2"          
               android:layout_margin="5dp"
              android:background="@android:color/transparent"
              android:textSize="26sp" 
    
                  android:text="b"/>
    
    
            </TableRow>
             <TableRow >
            <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/b3"          
               android:layout_margin="5dp"
              android:background="@android:color/transparent"
              android:textSize="26sp" 
                  android:text="1"/>
              <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/b4"          
               android:layout_margin="5dp"
              android:background="@android:color/transparent"
              android:textSize="26sp" 
    
                  android:text="2"/>
    
            </TableRow> <TableRow >
              <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/b5"          
               android:layout_margin="5dp"
              android:background="@android:color/transparent"
              android:textSize="26sp" 
    
                  android:text="A"/>
              <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/b6"          
               android:layout_margin="5dp"
              android:background="@android:color/transparent"
              android:textSize="26sp" 
    
                  android:text="B"/>
    
            </TableRow>
    </TableLayout>
    

      

    MainActivity.java

              package com.example.in;
    
                      import android.os.Build;
    
                     import android.os.Bundle;
    
                import android.annotation.SuppressLint;
    
              import android.annotation.TargetApi;
            import android.app.Activity;
              import android.content.ClipData;
               import android.graphics.Color;
          import android.graphics.Typeface;
           import android.view.DragEvent;
             import android.view.Menu;
            import android.view.MotionEvent;
             import android.view.View;
            import android.view.View.DragShadowBuilder;
            import android.view.View.OnDragListener;
           import android.view.View.OnTouchListener;
            import android.widget.Button;
          import android.widget.TextView;
         import android.widget.Toast;
    
      public class MainActivity extends Activity {
    Button b1,b2,b3,b4,b5,b6;
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final String BTN_TAG ="BUTTON";
        setContentView(R.layout.activity_main);
    
    
    
         Button b1=(Button) findViewById(R.id.b1);
    
        Button b2=(Button) findViewById(R.id.b2);
    
        Button b3=(Button) findViewById(R.id.b3);
    
        Button b4=(Button) findViewById(R.id.b4);
    
        Button b5=(Button) findViewById(R.id.b5);
    
        Button b6=(Button) findViewById(R.id.b6);
    
    
        //b1.setTag(BTN_TAG);
    
    
        b1.setOnTouchListener(new ChoiceTouchListener());
        b2.setOnTouchListener(new ChoiceTouchListener());
        b3.setOnTouchListener(new ChoiceTouchListener());
    
    
        b4.setOnDragListener(new ChoiceDragListener());
        b5.setOnDragListener(new ChoiceDragListener());
        b6.setOnDragListener(new ChoiceDragListener());
    
    }
    @SuppressWarnings("unused")
    private final class ChoiceTouchListener implements OnTouchListener {
        @SuppressLint("NewApi")
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
    
                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
    
                view.startDrag(data, shadowBuilder, view, 0);
                return true;
            } else {
                return false;
            }
        }
    } 
    
    @SuppressLint("NewApi")
    private class ChoiceDragListener implements OnDragListener {
    
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
    
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
    
    
                break;
            case DragEvent.ACTION_DRAG_EXITED:    
    
                //no action necessary
                break;
            case DragEvent.ACTION_DROP:
    
    
                View view = (View) event.getLocalState();
    
                Button dropTarget = (Button) v;
    
                Button dropped = (Button) view;
                dropTarget.setText(dropTarget.getText().toString() + dropped.getText().toString());
            case DragEvent.ACTION_DRAG_ENDED:
                //no action necessary
                break;
            default:
                break;
            }
            return true;
        }
    } 
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    

    }

2 个答案:

答案 0 :(得分:0)

  1. 您可以使用本地计数变量来计算拖放次数。
  2. 您可以在切换案例陈述中使用v.getId();来获取拖放视图的ID ...

答案 1 :(得分:0)

使用地图跟踪每个项目被拖动的次数

Map<Integer, Integer> dragMap = new HashMap<Integer, Integer>();

onDrag()方法中,您可以向dragMap添加新项目或增加与现有项目相关的计数

if (dragMap.get(dragTarget.getId() == null) {
    dragMap.put(dragTarget.getId(), 1);
}
else {
    dragMap.put(dragTarget.getId(), 
                dragMap.get(dragTarget.getId()++);
}

您需要一些额外的代码来检查项目被拖动的次数,如果超过某个数字,则阻止拖动操作。 此代码将放在DragEvent.ACTION_DRAG_STARTED:块中。