我正在尝试制作一个带有拖放功能的应用程序,但我无法从文本视图中拖动文本到另一个文本视图,但使用以下代码可以从文本视图拖动到编辑文本。
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity implements OnLongClickListener {
private TextView textView;
private TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
myDragEventListener mDragListen = new myDragEventListener();
textView.setTag("Example");
textView.setOnLongClickListener(this);
textView1 = (TextView)findViewById(R.id.textView1);
textView1.setOnDragListener(mDragListen);
}
protected class myDragEventListener implements View.OnDragListener
{
@Override
public boolean onDrag(View v, DragEvent event)
{
final int action = event.getAction();
switch(action)
{
case DragEvent.ACTION_DRAG_STARTED:
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
{
return true;
}
// Returns false. During the current drag and drop operation, this View will
// not receive events again until ACTION_DRAG_ENDED is sent.
return false;
case DragEvent.ACTION_DRAG_ENTERED:
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore the event
return true;
case DragEvent.ACTION_DRAG_EXITED:
return true;
case DragEvent.ACTION_DROP:
if(v == findViewById(R.id.textView1))
{
TextView view = (TextView) event.getLocalState();
textView1.setText(view.getText().toString());
}
else
{
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "You can't drop the image here", Toast.LENGTH_LONG).show();
break;
}
break;
case DragEvent.ACTION_DRAG_ENDED:
v.invalidate();
// Does a getResult(), and displays what happened.
if (event.getResult())
{
Toast.makeText(MainActivity.this, "The drop was handled.", Toast.LENGTH_LONG).show();
}
else
{
}
// returns true; the value is ignored.
return true;
// An unknown action type was received.
default:
Log.e("DragDrop Example","Unknown action type received by OnDragListener.");
break;
}
return false;
}
}
@Override
public boolean onLongClick(View view)
{
TextView textView = (TextView) view;
ClipData.Item item = new ClipData.Item(textView.getText().toString());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData("", mimeTypes, item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag( data,
shadowBuilder,//data to be dragged
view, //local data about the drag and drop operation
0 //no needed flags
);
return true;
};
}
我的错误在哪里?