如何TextView的拖动到正确的目标其他的TextView其他的、拖动、正确、目标

2023-09-05 02:39:37 作者:*未緍大男人

我已经给全code,我有。

在code波纹管可以通过拖放操作很顺利,但它不能正常工作像什么,我需要像波纹​​管的说明。

将数据是(苹果,ORAGE,球) 删除目标位置是(一种,O代表,B代表)

所以,如果拖苹果下降到一种这将是正确的,更新的文字为一种用于苹果和文本苹果在拖动会隐藏...但如果​​我们拖动苹果降为O代表或B的,有人会的球B失败,因为O代表橙和

根据在code娄我应该怎么补充。

布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:填充=10dp
机器人:以下属性来=50dp
机器人:paddingRight =50dp>


<的TextView
    机器人:ID =@ + ID / option_1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =5DP
    机器人:背景=@可绘制/选项
    机器人:重力=中心
    机器人:文本=苹果
    机器人:TEXTSTYLE =黑体/>

<的TextView
    机器人:ID =@ + ID / option_2
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =5DP
    机器人:背景=@可绘制/选项
    机器人:重力=中心
    机器人:文本=橙色
    机器人:TEXTSTYLE =黑体/>

<的TextView
    机器人:ID =@ + ID / option_3
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =5DP
    机器人:背景=@可绘制/选项
    机器人:重力=中心
    机器人:文本=球
    机器人:TEXTSTYLE =黑体/>

<的TextView
    机器人:ID =@ + ID / choice_1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =5DP
    机器人:背景=@可绘制/选择
    机器人:重力=中心
    机器人:文本=一种/>

<的TextView
    机器人:ID =@ + ID / choice_2
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =5DP
    机器人:背景=@可绘制/选择
    机器人:重力=中心
    机器人:文本=O代表/>

<的TextView
    机器人:ID =@ + ID / choice_3
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =5DP
    机器人:背景=@可绘制/选择
    机器人:重力=中心
    机器人:文本=B为/>

< / LinearLayout中>
 

Acvitity:

 公共类picture_to_word_24_color延伸活动{

//文本视图被拖拽到
私人TextView的选项1,选项2,选项3,choice1,choice2,choice3;
公众的CharSequence dragData;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.picture_to_word_24_color);

    //得到两套文本视图

    //意见阻力
    选项​​1 =(TextView中)findViewById(R.id.option_1);
    选项​​2 =(TextView中)findViewById(R.id.option_2);
    2选项=(TextView中)findViewById(R.id.option_3);

    //观点下降到
    choice1 =(TextView中)findViewById(R.id.choice_1);
    choice2 =(TextView中)findViewById(R.id.choice_2);
    choice3 =(TextView中)findViewById(R.id.choice_3);

    //设置触摸监听器
    option1.setOnTouchListener(新ChoiceTouchListener());
    option2.setOnTouchListener(新ChoiceTouchListener());
    option3.setOnTouchListener(新ChoiceTouchListener());

    //设置拖动听众
    choice1.setOnDragListener(新ChoiceDragListener());
    choice2.setOnDragListener(新ChoiceDragListener());
    choice3.setOnDragListener(新ChoiceDragListener());
}

/ **
 * ChoiceTouchListener将处理上拖动看法触摸事件
 *
 * /
私人final类ChoiceTouchListener实现OnTouchListener {
    @燮pressLint(NewApi)
    公共布尔onTouch(查看视图,MotionEvent motionEvent){
        如果(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
            / *
             *拖动细节:我们只需要默认行为
             *  - 剪辑数据可以被设置为传递数据作为拖动的一部分
             *  - 阴影可定制
             * /
            ClipData数据= ClipData.newPlainText(,);
            DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(视图);
            //开始拖动该项目打动
            view.startDrag(数据,shadowBuilder,视图,0);
            返回true;
        } 其他 {
            返回false;
        }
    }
}

/ **
 * DragListener将处理拖意见从山的拖放区域
 *  - 只有拖放操作将已处理添加到它,因为我们都没有
 *  - 修正了拖拽过程中的其他部分的默认行为
 *
 * /
@燮pressLint(NewApi)
私有类ChoiceDragListener实现OnDragListener {

    @覆盖
    公共ondrag当布尔(视图V,的dragEvent事件){
        开关(event.getAction()){
        案例DragEvent.ACTION_DRAG_STARTED:
            //没有必要行动
            打破;
        案例DragEvent.ACTION_DRAG_ENTERED:
            //没有必要行动
            Toast.makeText(picture_to_word_24_color.thisACTION_DRAG_ENTERED,Toast.LENGTH_SHORT).show();
            打破;
        案例DragEvent.ACTION_DRAG_EXITED:
            //没有必要行动
            Toast.makeText(picture_to_word_24_color.thisACTION_DRAG_EXITED,Toast.LENGTH_SHORT).show();
            打破;
        案例DragEvent.ACTION_DROP:

            Toast.makeText(picture_to_word_24_color.thisACTION_DROP,Toast.LENGTH_SHORT).show();
            //处理拖观点被丢弃在一个下降视图
            查看查看=(查看)event.getLocalState();
            //停止显示视图,这是它被拖到前
            view.setVisibility(View.INVISIBLE);
            //观点拖动项目是被丢弃在
            TextView中的DropTarget =(TextView中)V;
            //观点被拖放
            TextView的下降=(TextView的)观点;
            //更新目标视图中的文本,以反映该数据被丢弃
            dropTarget.setText(dropped.getText());
            //使其大胆地强调的是,一个项目已被删除
            dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
            //如果一个项已被丢弃这里,将有一个标签
            对象标记= dropTarget.getTag();
            //如果已经有一个项目在这里,将其放回原处可见
            如果(标记!= NULL)
            {
                //标签是视图id已经下降在这里
                INT existingID =(整数)标签;
                //重新设置可见原来的视图
                findViewById(existingID).setVisibility(View.VISIBLE);
            }
            //设置目标视图标签被丢弃的 - 到视图的ID被丢弃
            dropTarget.setTag(dropped.getId());
            打破;
        案例DragEvent.ACTION_DRAG_ENDED:
            //没有必要行动
            Toast.makeText(picture_to_word_24_color.thisACTION_DRAG_ENDED,Toast.LENGTH_SHORT).show();
            打破;
        默认:
            打破;
        }
        返回true;
    }
}
}
 
Android02之TextView和Button

解决方案

请尝试以下code,应根据问题的说明工作。 我已经增加了一个额外的事情,即重置按钮,我觉得有必要。

picture_to_word_24_color.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直
机器人:填充=10dp
机器人:以下属性来=50dp
机器人:paddingRight =50dp>


<的TextView
机器人:ID =@ + ID / option_1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5DP
机器人:背景=@可绘制/选项
机器人:重力=中心
机器人:文本=苹果
机器人:TEXTSTYLE =黑体/>

<的TextView
机器人:ID =@ + ID / option_2
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5DP
机器人:背景=@可绘制/选项
机器人:重力=中心
机器人:文本=橙色
机器人:TEXTSTYLE =黑体/>

<的TextView
机器人:ID =@ + ID / option_3
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5DP
机器人:背景=@可绘制/选项
机器人:重力=中心
机器人:文本=球
机器人:TEXTSTYLE =黑体/>

<的TextView
机器人:ID =@ + ID / choice_1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5DP
机器人:背景=@可绘制/选择
机器人:重力=中心
机器人:文本=一种/>

<的TextView
机器人:ID =@ + ID / choice_2
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5DP
机器人:背景=@可绘制/选择
机器人:重力=中心
机器人:文本=O代表/>

<的TextView
机器人:ID =@ + ID / choice_3
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_margin =5DP
机器人:背景=@可绘制/选择
机器人:重力=中心
机器人:文本=B为/>

<按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=复位
机器人:的onClick =重置/>

< / LinearLayout中>
 

Picture_to_word_24_color.java

 包com.example.touchanddrag;

进口android.os.Build;
进口android.os.Bundle;
进口android.annotation.Sup pressLint;
进口android.annotation.TargetApi;
进口android.app.Activity;
进口android.content.ClipData;
进口android.graphics.Typeface;
进口android.view.DragEvent;
进口android.view.Menu;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.DragShadowBuilder;
进口android.view.View.OnDragListener;
进口android.view.View.OnTouchListener;
进口android.widget.TextView;
进口android.widget.Toast;

@燮pressLint(NewApi)
公共类Picture_to_word_24_color延伸活动{

私人TextView的选项1,选项2,选项3,choice1,choice2,choice3;
公众的CharSequence dragData;

@TargetApi(Build.VERSION_ codeS.HONEYCOMB)
@燮pressLint(NewApi)
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.picture_to_word_24_color);

    //得到两套文本视图

    //意见阻力
    选项​​1 =(TextView中)findViewById(R.id.option_1);
    选项​​2 =(TextView中)findViewById(R.id.option_2);
    2选项=(TextView中)findViewById(R.id.option_3);

    //观点下降到
    choice1 =(TextView中)findViewById(R.id.choice_1);
    choice2 =(TextView中)findViewById(R.id.choice_2);
    choice3 =(TextView中)findViewById(R.id.choice_3);

    //设置触摸监听器
    option1.setOnTouchListener(新ChoiceTouchListener());
    option2.setOnTouchListener(新ChoiceTouchListener());
    option3.setOnTouchListener(新ChoiceTouchListener());

    //设置拖动听众
    choice1.setOnDragListener(新ChoiceDragListener());
    choice2.setOnDragListener(新ChoiceDragListener());
    choice3.setOnDragListener(新ChoiceDragListener());
}

/ **
 * ChoiceTouchListener将处理上拖动看法触摸事件
 *
 * /
私人final类ChoiceTouchListener实现OnTouchListener {
    @燮pressLint(NewApi)
    @覆盖
    公共布尔onTouch(查看视图,MotionEvent motionEvent){
        如果(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
            / *
             *拖动细节:我们只需要默认行为
             *  - 剪辑数据可以被设置为传递数据作为拖动的一部分
             *  - 阴影可定制
             * /
            ClipData数据= ClipData.newPlainText(,);
            DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(视图);
            //开始拖动该项目打动
            view.startDrag(数据,shadowBuilder,视图,0);
            返回true;
        } 其他 {
            返回false;
        }
    }
}

/ **
 * DragListener将处理拖意见从山的拖放区域
 *  - 只有拖放操作将已处理添加到它,因为我们都没有
 *  - 修正了拖拽过程中的其他部分的默认行为
 *
 * /
@燮pressLint(NewApi)
私有类ChoiceDragListener实现OnDragListener {

    @覆盖
    公共ondrag当布尔(视图V,的dragEvent事件){
        开关(event.getAction()){
        案例DragEvent.ACTION_DRAG_STARTED:
            //没有必要行动
            打破;
        案例DragEvent.ACTION_DRAG_ENTERED:
            //没有必要行动
            打破;
        案例DragEvent.ACTION_DRAG_EXITED:
            //没有必要行动
            打破;
        案例DragEvent.ACTION_DROP:

            //处理拖观点被丢弃在一个下降视图
            查看查看=(查看)event.getLocalState();
            //观点拖动项目是被丢弃在
            TextView中的DropTarget =(TextView中)V;
            //观点被拖放
            TextView的下降=(TextView的)观点;
            //检查的DropTarget的第一个字符是否等于放弃第一个字符
            如果(dropTarget.getText()。的toString()。的charAt(0)== dropped.getText()。的toString()。的charAt(0))
            {
                //停止显示视图,这是它被拖到前
                view.setVisibility(View.INVISIBLE);
                //更新目标视图中的文本,以反映该数据被丢弃
                dropTarget.setText(。dropTarget.getText()的toString()+ dropped.getText()的toString());
                //使其大胆地强调的是,一个项目已被删除
                dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
                //如果一个项已被丢弃这里,将有一个标签
                对象标记= dropTarget.getTag();
                //如果已经有一个项目在这里,将其放回原处可见
                如果(标记!= NULL)
                {
                    //标签是视图id已经下降在这里
                    INT existingID =(整数)标签;
                    //重新设置可见原来的视图
                    findViewById(existingID).setVisibility(View.VISIBLE);
                }
                //设置目标视图标签被丢弃的 - 到视图的ID被丢弃
                dropTarget.setTag(dropped.getId());
                //通过OnDragListener设置为null,删除setOnDragListener所以没有进一步的阻力和放大器;丢弃在此的TextView可以做到
                dropTarget.setOnDragListener(空);
            }
            其他
                //显示信息,如果DropTarget的第一个字符是不是等于放弃第一个字符
                Toast.makeText(Picture_to_word_24_color.this,dropTarget.getText()的toString()+不是+ dropped.getText()的toString(),Toast.LENGTH_LONG。).show();
            打破;
        案例DragEvent.ACTION_DRAG_ENDED:
            //没有必要行动
            打破;
        默认:
            打破;
        }
        返回true;
    }
}

公共无效复位(查看视图)
{
    option1.setVisibility(TextView.VISIBLE);
    option2.setVisibility(TextView.VISIBLE);
    option3.setVisibility(TextView.VISIBLE);

    choice1.setText(一种);
    choice2.setText(O的);
    choice3.setText(乙对于);

    choice1.setTag(空);
    choice2.setTag(空);
    choice3.setTag(空);

    choice1.setTypeface(Typeface.DEFAULT);
    choice2.setTypeface(Typeface.DEFAULT);
    choice3.setTypeface(Typeface.DEFAULT);

    choice1.setOnDragListener(新ChoiceDragListener());
    choice2.setOnDragListener(新ChoiceDragListener());
    choice3.setOnDragListener(新ChoiceDragListener());
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.picture_to_word_24_color,菜单);
    返回true;
}

}
 

不要忘记检查目标API级别。当我不得不添加 @燮pressLint(NewApi)

I have giving the full code that I have.

The code bellow can drag and drop very smoothly but it is not working like what I need like bellow description.

Drag Data Is (Apple, Orage, Ball) Drop target location is (A for, O for, B for)

So, if drag "Apple" drop to "A for" it will be CORRECT and update text to "A for Apple" and Text "Apple" at drag will be hide... but if we drag "Apple" drop to "O for" or "B for" it will be failed because "O for Orange" and "B for Ball".

Based on the code bellow what should I add.

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="50dp"
android:paddingRight="50dp" >


<TextView
    android:id="@+id/option_1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/option"
    android:gravity="center"
    android:text="Apple"
    android:textStyle="bold" />

<TextView
    android:id="@+id/option_2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/option"
    android:gravity="center"
    android:text="Orange"
    android:textStyle="bold" />

<TextView
    android:id="@+id/option_3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/option"
    android:gravity="center"
    android:text="Ball"
    android:textStyle="bold" />

<TextView
    android:id="@+id/choice_1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/choice"
    android:gravity="center"
    android:text="A for " />

<TextView
    android:id="@+id/choice_2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/choice"
    android:gravity="center"
    android:text="O for" />

<TextView
    android:id="@+id/choice_3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/choice"
    android:gravity="center"
    android:text="B for " />

</LinearLayout>

Acvitity:

public class picture_to_word_24_color extends Activity {

//text views being dragged and dropped onto
private TextView option1, option2, option3, choice1, choice2, choice3;
public CharSequence dragData;

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

    //get both sets of text views

    //views to drag
    option1 = (TextView)findViewById(R.id.option_1);
    option2 = (TextView)findViewById(R.id.option_2);
    option3 = (TextView)findViewById(R.id.option_3);

    //views to drop onto
    choice1 = (TextView)findViewById(R.id.choice_1);
    choice2 = (TextView)findViewById(R.id.choice_2);
    choice3 = (TextView)findViewById(R.id.choice_3);

    //set touch listeners
    option1.setOnTouchListener(new ChoiceTouchListener());
    option2.setOnTouchListener(new ChoiceTouchListener());
    option3.setOnTouchListener(new ChoiceTouchListener());

    //set drag listeners
    choice1.setOnDragListener(new ChoiceDragListener());
    choice2.setOnDragListener(new ChoiceDragListener());
    choice3.setOnDragListener(new ChoiceDragListener());
}

/**
 * ChoiceTouchListener will handle touch events on draggable views
 *
 */
private final class ChoiceTouchListener implements OnTouchListener {
    @SuppressLint("NewApi")
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            /*
             * Drag details: we only need default behavior
             * - clip data could be set to pass data as part of drag
             * - shadow can be tailored
             */
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            //start dragging the item touched
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        } else {
            return false;
        }
    }
} 

/**
 * DragListener will handle dragged views being dropped on the drop area
 * - only the drop action will have processing added to it as we are not
 * - amending the default behavior for other parts of the drag process
 *
 */
@SuppressLint("NewApi")
private class ChoiceDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            //no action necessary
            Toast.makeText(picture_to_word_24_color.this, "ACTION_DRAG_ENTERED", Toast.LENGTH_SHORT).show();
            break;
        case DragEvent.ACTION_DRAG_EXITED:        
            //no action necessary
            Toast.makeText(picture_to_word_24_color.this, "ACTION_DRAG_EXITED", Toast.LENGTH_SHORT).show();
            break;
        case DragEvent.ACTION_DROP:

            Toast.makeText(picture_to_word_24_color.this, "ACTION_DROP", Toast.LENGTH_SHORT).show();
            //handle the dragged view being dropped over a drop view
            View view = (View) event.getLocalState();
            //stop displaying the view where it was before it was dragged
            view.setVisibility(View.INVISIBLE);
            //view dragged item is being dropped on
            TextView dropTarget = (TextView) v;
            //view being dragged and dropped
            TextView dropped = (TextView) view;
            //update the text in the target view to reflect the data being dropped
            dropTarget.setText(dropped.getText());
            //make it bold to highlight the fact that an item has been dropped
            dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
            //if an item has already been dropped here, there will be a tag
            Object tag = dropTarget.getTag();
            //if there is already an item here, set it back visible in its original place
            if(tag!=null)
            {
                //the tag is the view id already dropped here
                int existingID = (Integer)tag;
                //set the original view visible again
                findViewById(existingID).setVisibility(View.VISIBLE);
            }
            //set the tag in the target view being dropped on - to the ID of the view being dropped
            dropTarget.setTag(dropped.getId());
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            //no action necessary
            Toast.makeText(picture_to_word_24_color.this, "ACTION_DRAG_ENDED", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
        return true;
    }
} 
}

解决方案

Try the following code, it should work according to your description in the question. I have added an extra thing i.e. reset button as I felt it necessary.

picture_to_word_24_color.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="50dp"
android:paddingRight="50dp" >


<TextView
android:id="@+id/option_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/option"
android:gravity="center"
android:text="Apple"
android:textStyle="bold" />

<TextView
android:id="@+id/option_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/option"
android:gravity="center"
android:text="Orange"
android:textStyle="bold" />

<TextView
android:id="@+id/option_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/option"
android:gravity="center"
android:text="Ball"
android:textStyle="bold" />

<TextView
android:id="@+id/choice_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/choice"
android:gravity="center"
android:text="A for " />

<TextView
android:id="@+id/choice_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/choice"
android:gravity="center"
android:text="O for " />

<TextView
android:id="@+id/choice_3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/choice"
android:gravity="center"
android:text="B for " />

<Button 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:onClick="reset"/>

</LinearLayout>

Picture_to_word_24_color.java

package com.example.touchanddrag;

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.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.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class Picture_to_word_24_color extends Activity {

private TextView option1, option2, option3, choice1, choice2, choice3;
public CharSequence dragData;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_to_word_24_color);

    //get both sets of text views

    //views to drag
    option1 = (TextView)findViewById(R.id.option_1);
    option2 = (TextView)findViewById(R.id.option_2);
    option3 = (TextView)findViewById(R.id.option_3);

    //views to drop onto
    choice1 = (TextView)findViewById(R.id.choice_1);
    choice2 = (TextView)findViewById(R.id.choice_2);
    choice3 = (TextView)findViewById(R.id.choice_3);

    //set touch listeners
    option1.setOnTouchListener(new ChoiceTouchListener());
    option2.setOnTouchListener(new ChoiceTouchListener());
    option3.setOnTouchListener(new ChoiceTouchListener());

    //set drag listeners
    choice1.setOnDragListener(new ChoiceDragListener());
    choice2.setOnDragListener(new ChoiceDragListener());
    choice3.setOnDragListener(new ChoiceDragListener());
}

/**
 * ChoiceTouchListener will handle touch events on draggable views
 *
 */
private final class ChoiceTouchListener implements OnTouchListener {
    @SuppressLint("NewApi")
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            /*
             * Drag details: we only need default behavior
             * - clip data could be set to pass data as part of drag
             * - shadow can be tailored
             */
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            //start dragging the item touched
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        } else {
            return false;
        }
    }
} 

/**
 * DragListener will handle dragged views being dropped on the drop area
 * - only the drop action will have processing added to it as we are not
 * - amending the default behavior for other parts of the drag process
 *
 */
@SuppressLint("NewApi")
private class ChoiceDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:        
            //no action necessary
            break;
        case DragEvent.ACTION_DROP:

            //handle the dragged view being dropped over a drop view
            View view = (View) event.getLocalState();
            //view dragged item is being dropped on
            TextView dropTarget = (TextView) v;
            //view being dragged and dropped
            TextView dropped = (TextView) view;
            //checking whether first character of dropTarget equals first character of dropped
            if(dropTarget.getText().toString().charAt(0) == dropped.getText().toString().charAt(0))
            {
                //stop displaying the view where it was before it was dragged
                view.setVisibility(View.INVISIBLE);
                //update the text in the target view to reflect the data being dropped
                dropTarget.setText(dropTarget.getText().toString() + dropped.getText().toString());
                //make it bold to highlight the fact that an item has been dropped
                dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
                //if an item has already been dropped here, there will be a tag
                Object tag = dropTarget.getTag();
                //if there is already an item here, set it back visible in its original place
                if(tag!=null)
                {
                    //the tag is the view id already dropped here
                    int existingID = (Integer)tag;
                    //set the original view visible again
                    findViewById(existingID).setVisibility(View.VISIBLE);
                }
                //set the tag in the target view being dropped on - to the ID of the view being dropped
                dropTarget.setTag(dropped.getId());
                //remove setOnDragListener by setting OnDragListener to null, so that no further drag & dropping on this TextView can be done
                dropTarget.setOnDragListener(null);
            }
            else
                //displays message if first character of dropTarget is not equal to first character of dropped
                Toast.makeText(Picture_to_word_24_color.this, dropTarget.getText().toString() + "is not " + dropped.getText().toString(), Toast.LENGTH_LONG).show();
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            //no action necessary
            break;
        default:
            break;
        }
        return true;
    }
} 

public void reset(View view)
{
    option1.setVisibility(TextView.VISIBLE);
    option2.setVisibility(TextView.VISIBLE);
    option3.setVisibility(TextView.VISIBLE);

    choice1.setText("A for ");
    choice2.setText("O for ");
    choice3.setText("B for ");

    choice1.setTag(null);
    choice2.setTag(null);
    choice3.setTag(null);

    choice1.setTypeface(Typeface.DEFAULT);
    choice2.setTypeface(Typeface.DEFAULT);
    choice3.setTypeface(Typeface.DEFAULT);

    choice1.setOnDragListener(new ChoiceDragListener());
    choice2.setOnDragListener(new ChoiceDragListener());
    choice3.setOnDragListener(new ChoiceDragListener());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.picture_to_word_24_color, menu);
    return true;
}

}

Don't forget to check for target API level. As I had to add @SuppressLint("NewApi")

 
精彩推荐
图片推荐