gpt4 book ai didi

android - 将 TextView 的文本拖放到 TextView

转载 作者:太空狗 更新时间:2023-10-29 15:00:01 26 4
gpt4 key购买 nike

我正在尝试制作具有拖放功能的应用程序,但我无法将文本从 textview 拖动到 textview,但可以使用此代码从 TextView 拖动文本以编辑文本。

@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;
};
}

我的错误在哪里?

最佳答案

使用onTouchListener() 代替longClickListener()。如果它不起作用,请告诉我

textView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("datalabel", "text");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);
v.startDrag(data, shadow, null, 0);
return false;
}
});

关于android - 将 TextView 的文本拖放到 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27688476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com