android - 如何在textview中只在一个textview中堆叠文本?

时间:2017-04-13 14:08:58

标签: android drag-and-drop textview

我正在这个项目中工作,用户可以自定义自己的披萨。我想要实现的是当用户将披萨放在披萨面团上时,文本视图上会显示一个文本。例如:当用户拖动ONION并将其放在披萨面团上时,textview将显示ONION。其他浇头也一样。虽然我已经完成了它,但它并没有堆叠。就像,当我放弃ONION和PEPPERONI时,textview会

ONIONPEPPERONI

ONION
PEPPERONI

这是代码。我不知道堆叠文本的正确术语,所以我无法解释它。

公共类MainActivity扩展了AppCompatActivity {

Button bCheese, bMoz, bOnion, bPatty, bPep, bGreenBell, bMush, bPine, bHam, bOlive, bBacon;
TextView txt;
ImageView image, image2, image3, image4, image5, image6, image7, image8, image9, image10, image11, image12;

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

    bCheese = (Button) findViewById(R.id.imageCheese);
    bMoz = (Button) findViewById(R.id.imageMoz);
    bOnion = (Button) findViewById(R.id.imageOnion);
    bPatty = (Button) findViewById(R.id.imagePatty);
    bPep = (Button) findViewById(R.id.imagePepperoni);
    bGreenBell = (Button) findViewById(R.id.imageGreen);
    bMush = (Button) findViewById(R.id.imageMush);
    bPine = (Button) findViewById(R.id.imagePine);
    bHam = (Button) findViewById(R.id.imageHam);
    bOlive = (Button) findViewById(R.id.imageOlive);
    bBacon = (Button) findViewById(R.id.imageBacon);


    txt = (TextView) findViewById(R.id.textView);
    image = (ImageView) findViewById(R.id.imageView);
    image2 = (ImageView) findViewById(R.id.imageView2);
    image3 = (ImageView) findViewById(R.id.imageView3);
    image4 = (ImageView) findViewById(R.id.imageView4);
    image5 = (ImageView) findViewById(R.id.imageView5);
    image6 = (ImageView) findViewById(R.id.imageView6);
    image7 = (ImageView) findViewById(R.id.imageView7);
    image8 = (ImageView) findViewById(R.id.imageView8);
    image9 = (ImageView) findViewById(R.id.imageView9);
    image10 = (ImageView) findViewById(R.id.imageView10);
    image11 = (ImageView) findViewById(R.id.imageView11);
    image12 = (ImageView) findViewById(R.id.imageView12);


    bCheese.setOnLongClickListener(longClickListener);
    bMoz.setOnLongClickListener(longClickListener);
    bOnion.setOnLongClickListener(longClickListener);
    bPatty.setOnLongClickListener(longClickListener);
    bPep.setOnLongClickListener(longClickListener);
    bGreenBell.setOnLongClickListener(longClickListener);
    bMush.setOnLongClickListener(longClickListener);
    bPine.setOnLongClickListener(longClickListener);
    bHam.setOnLongClickListener(longClickListener);
    bOlive.setOnLongClickListener(longClickListener);
    bBacon.setOnLongClickListener(longClickListener);

    image.setOnDragListener(dragListener);
}

View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        ClipData data = ClipData.newPlainText("","");
        View.DragShadowBuilder myShadowBuilder = new View.DragShadowBuilder(v);
        v.startDrag(data,myShadowBuilder,v,0);
        return true;
    }
};

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

        final View view = (View) event.getLocalState();

        switch(dragEvent){
            case DragEvent.ACTION_DRAG_ENTERED:

                if (view.getId() == R.id.imageCheese){
                    image2.setImageResource(R.drawable.cheddarpic);
                }

                else if (view.getId() == R.id.imageMoz){
                    image3.setImageResource(R.drawable.mozarellapic);
                }

                else if (view.getId() == R.id.imageOnion){
                    image4.setImageResource(R.drawable.onionspic);
                }

                else if (view.getId() == R.id.imagePatty){
                    image5.setImageResource(R.drawable.pattiespic);
                }

                else if (view.getId() == R.id.imagePepperoni){
                    image6.setImageResource(R.drawable.pepperonipic);
                }

                else if (view.getId() == R.id.imageGreen){
                    image7.setImageResource(R.drawable.greenbellpic);
                }

                else if (view.getId() == R.id.imageMush){
                    image8.setImageResource(R.drawable.mushroomspic);
                }

                else if (view.getId() == R.id.imagePine){
                    image9.setImageResource(R.drawable.pineapplespic);
                }

                else if (view.getId() == R.id.imageHam){
                    image10.setImageResource(R.drawable.hampic);
                }

                else if (view.getId() == R.id.imageOlive){
                    image11.setImageResource(R.drawable.olivespic);
                }

                else if (view.getId() == R.id.imageBacon){
                    image12.setImageResource(R.drawable.baconpic);
                }

                Log.i("DRAG", "ACTION DRAG ENTERED");
                break;
            case DragEvent.ACTION_DRAG_EXITED:

                if (view.getId() == R.id.imageCheese){
                    image2.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imageMoz){
                    image3.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imageOnion){
                    image4.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imagePatty){
                    image5.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imagePepperoni){
                    image6.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imageGreen){
                    image7.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imageMush){
                    image8.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imagePine){
                    image9.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imageHam){
                    image10.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imageOlive){
                    image11.setImageResource(R.drawable.pizzadough);
                }

                else if (view.getId() == R.id.imageBacon){
                    image12.setImageResource(R.drawable.pizzadough);
                }

                Log.i("DRAG", "ACTION DRAG EXITED");
                break;
            case DragEvent.ACTION_DROP:              
                if (view.getId() == R.id.imageCheese){
                    image2.setImageResource(R.drawable.cheddarpic);
                    txt.append("Cheddar Cheese");
                }

                else if (view.getId() == R.id.imageMoz){
                    image3.setImageResource(R.drawable.mozarellapic);
                    txt.append("Mozarella Cheese");
                }

                else if (view.getId() == R.id.imageOnion){
                    image4.setImageResource(R.drawable.onionspic);
                    txt.append("Onions");
                }

                else if (view.getId() == R.id.imagePatty){
                    image5.setImageResource(R.drawable.pattiespic);
                    txt.append("Patties");
                }

                else if (view.getId() == R.id.imagePepperoni){
                    image6.setImageResource(R.drawable.pepperonipic);
                    txt.append("Pepperoni");
                }

                else if (view.getId() == R.id.imageGreen){
                    image7.setImageResource(R.drawable.greenbellpic);
                    txt.append("Green Bell Peppers");
                }

                else if (view.getId() == R.id.imageMush){
                    image8.setImageResource(R.drawable.mushroomspic);
                    txt.append("Mushrooms");
                }

                else if (view.getId() == R.id.imagePine){
                    image9.setImageResource(R.drawable.pineapplespic);
                    txt.append("Pineapples");
                }

                else if (view.getId() == R.id.imageHam){
                    image10.setImageResource(R.drawable.hampic);
                    txt.append("Ham");
                }

                else if (view.getId() == R.id.imageOlive){
                    image11.setImageResource(R.drawable.olivespic);
                    txt.append("Olives");
                }

                else if (view.getId() == R.id.imageBacon){
                    image12.setImageResource(R.drawable.baconpic);
                    txt.append("Bacon");
                }
                break;
        }

        return true;
    }
};

}

1 个答案:

答案 0 :(得分:0)

  

在xml中,只需使用垂直方向的线性布局,并将可见性设置为不变。因此,当他们拖动第一个textview时,只需将第二个textview的可见性设置为visible,然后将文本设置为pepperoni。我希望这有帮助

<LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:text="Onion"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_width="wrap_content"
                android:text="Pepperoni"
                android:visibility="gone"
                android:layout_height="wrap_content" />
        </LinearLayout>