Android Studio:将拖放的TextView设置为拖动的对象

时间:2018-07-22 01:24:08

标签: java android android-studio drag-and-drop

我正在实现具有6个拖放TextView和6个拖放TextView的拖放接口。 可以将6个拖放视图中的任何一个拖放到6个拖放视图中的任何一个上。

一个例子是,如果tvNum1 = 13,tvNum2 = 16,tvNum3 = 9,那么我可以将其中任何一个拖放到tvSTR,tvDEX或tvINT上,并将相应的Drop文本设置为所拖动的任何东西。因此,如果将tvNum2拖动到tvSTR上,则tvSTR应该显示16。或者说,您决定将tvNum3拖动到tvSTR上,然后tvSTR应该显示9。每个Drag视图只能使用一次。

这是我当前的代码。我找不到一种使之成为拖放对象的文本的方式。我只能进行(一对一关联),以便将tvNum1拖到tvSTR上,然后tvSTR = 13。或者,如果将tvNum2拖到tvDEX上,则tvDEX = 16。

public class setCharacterRolls extends AppCompatActivity
{
    // Variable declarations
    private TextView tvNum1, tvNum2, tvNum3, tvNum4, tvNum5, tvNum6;
    private TextView tvSTR, tvDEX, tvINT, tvWIS, tvCHA, tvCON;
    String roll1, roll2, roll3, roll4, roll5, roll6;
    // String roll1Num, roll2Num, roll3Num, roll4Num, roll5Num, roll6Num;
    private Button bSubmit;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_character_rolls);

        // Get the previous intent
        Intent intent = getIntent();

        // Save the rolls from the previous screen onto a String to use later.
        roll1 = intent.getStringExtra("roll1");
        roll2 = intent.getStringExtra("roll2");
        roll3 = intent.getStringExtra("roll3");
        roll4 = intent.getStringExtra("roll4");
        roll5 = intent.getStringExtra("roll5");
        roll6 = intent.getStringExtra("roll6");

        // Get TextView id's
        tvNum1 = (TextView) findViewById(R.id.tDrag1);
        tvNum2 = (TextView) findViewById(R.id.tDrag2);
        tvNum3 = (TextView) findViewById(R.id.tDrag3);
        tvNum4 = (TextView) findViewById(R.id.tDrag4);
        tvNum5 = (TextView) findViewById(R.id.tDrag5);
        tvNum6 = (TextView) findViewById(R.id.tDrag6);

        tvSTR = (TextView) findViewById(R.id.tvSTR);
        tvDEX = (TextView) findViewById(R.id.tvDEX);
        tvINT = (TextView) findViewById(R.id.tvINT);
        tvWIS = (TextView) findViewById(R.id.tvWIS);
        tvCHA = (TextView) findViewById(R.id.tvCHA);
        tvCON = (TextView) findViewById(R.id.tvCON);

        // Set TextView to the roll numbers
        tvNum1.setText(roll1);
        tvNum2.setText(roll2);
        tvNum3.setText(roll3);
        tvNum4.setText(roll4);
        tvNum5.setText(roll5);
        tvNum6.setText(roll6);

        // Set TextView to be draggable
        tvNum1.setOnTouchListener(onTouch);
        tvNum2.setOnTouchListener(onTouch);
        tvNum3.setOnTouchListener(onTouch);
        tvNum4.setOnTouchListener(onTouch);
        tvNum5.setOnTouchListener(onTouch);
        tvNum6.setOnTouchListener(onTouch);

        // Targets to be dropped on
        tvSTR.setOnDragListener(dragListener);
        tvDEX.setOnDragListener(dragListener);
        tvINT.setOnDragListener(dragListener);
        tvWIS.setOnDragListener(dragListener);
        tvCHA.setOnDragListener(dragListener);
        tvCON.setOnDragListener(dragListener);


    }

    View.OnTouchListener onTouch = new View.OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent mEvent)
        {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuild = new View.DragShadowBuilder(v);
            v.startDrag(data, shadowBuild, v, 0);
            return true;
        }
    };


    View.OnDragListener dragListener = new View.OnDragListener()
    {

        @Override
        public boolean onDrag(View v, DragEvent event)
        {
            int dragEvent = event.getAction();

            switch(dragEvent)
            {
                case DragEvent.ACTION_DRAG_STARTED:
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    final View view = (View) event.getLocalState();
                    switch(view.getId())
                    {
                        // This is where I have the 1-to-1 correlation.
                        // I want to be able to drag any draggable texview and set it to whatever it was dropped on.
                        case R.id.tDrag1:
                            tvSTR.setText(roll1);
                            break;
                        case R.id.tDrag2:
                            tvDEX.setText(roll2); 
                            break;
                        default:
                            break;
                    }
                    break;
                case DragEvent.ACTION_DROP:
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    break;
                default:
                    break;

            }

            return true;
        }
    };
}

0 个答案:

没有答案