我有三种布局:顶部,左侧和右侧。在顶部布局上,我有EditText图标,当我将其拖到左侧布局时,将在左侧布局中创建一个EditText。
在左侧布局或右侧布局中使用EditText,我可以将其从左向右移动布局(或从右向左移动,不能从左/右移动到顶部)
我的问题:
1.当我在左侧布局中通过拖动创建EditText时,我通过在左侧布局中拖动来创建EditText,它工作得很好,我可以看到所有拖放日志操作。但是,如果我再次在左侧布局中创建另一个EditText,则不会收到ACTION_DRAG_STARTED和ACTION_DRAG_ENDED。
http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at60954PM_zps14a2d784.png
2.当我创建一个EditText太接近的EditText已在布局中创建时,ACTION_DROP未收到
http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at61336PM_zps98ab85f1.png
我的代码: XML:
<LinearLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:background="@color/Azure">
<ImageView
android:id="@+id/sticker"
android:padding="10dip"
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/note"
>
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal">
<AbsoluteLayout
android:id="@+id/left"
android:layout_width="500dip"
android:layout_height="fill_parent"
android:background="@color/OldLace">
</AbsoluteLayout>
<AbsoluteLayout
android:id="@+id/right"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@color/BurlyWood">
</AbsoluteLayout>
</LinearLayout>
</LinearLayout>
活性: 包com.tranductrinh.mydraganddrop;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.content.ClipData;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.sticker).setOnTouchListener(new MyTouchListener());
findViewById(R.id.left).setOnDragListener(new MyDragListener());
findViewById(R.id.right).setOnDragListener(new MyDragListener());
}
private class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
//view.setVisibility(View.INVISIBLE);
return true;
} else
return false;
}
}
private class MyLongTouchListener implements View.OnLongClickListener {
public boolean onLongClick(View view) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
//view.setVisibility(View.INVISIBLE);
return true;
}
}
class MyDragListener implements View.OnDragListener {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.i("INFORMATION ", "START");
return true;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundColor(getResources().getColor(R.color.YellowGreen));
Log.i("INFORMATION ", "ENTER");
return true;
case DragEvent.ACTION_DRAG_EXITED:
if (v == findViewById(R.id.left))
v.setBackgroundColor(getResources().getColor(R.color.OldLace));
else
if (v == findViewById(R.id.right))
v.setBackgroundColor(getResources().getColor(R.color.BurlyWood));
Log.i("INFORMATION ", "EXIT");
return true;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
Log.i("INFORMATION ", "DROP");
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
if (owner.getId() != R.id.top) {
// remove Sticker in from layout
owner.removeView(view);
// add Sticker to new layout
AbsoluteLayout container = (AbsoluteLayout) v;
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(200, 200, (int)event.getX()-100, (int)event.getY()-100);
container.addView(view, params);
view.setVisibility(View.VISIBLE);
view.setFocusable(false);
// set onLongClickListener
view.setOnLongClickListener(new MyLongTouchListener());
Log.i("INFORMATION ", "Move operation");
}
else if (owner.getId() == R.id.top) {
//view.setVisibility(View.VISIBLE);
// new EditText object by code
EditText editText = new EditText(Main.this);
editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.note));
// add Sticker to Container
AbsoluteLayout container = (AbsoluteLayout) v;
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(200, 200, (int)event.getX()-100, (int)event.getY()-100);
container.addView(editText, params);
// set focus on EditText
editText.setFocusable(false);
// set onLongClickListener
editText.setOnLongClickListener(new MyLongTouchListener());
Log.i("INFORMATION ", "Copy operation");
}
// set view parent to normal
if (v == findViewById(R.id.left)) {
v.setBackgroundColor(getResources().getColor(R.color.OldLace));
}
else
if (v == findViewById(R.id.right)) {
v.setBackgroundColor(getResources().getColor(R.color.BurlyWood));
}
return true;
case DragEvent.ACTION_DRAG_ENDED:
Log.i("INFORMATION ", "Drag Drop Result : " + event.getResult());
if (event.getResult() == false) {
Toast toast = Toast.makeText(Main.this, "Invalid operation drag and drop", Toast.LENGTH_SHORT);
toast.show();
// when drop action not handler
((View) event.getLocalState()).setVisibility(View.VISIBLE);
}
Log.i("INFORMATION ", "END *******************");
return true;
default:
break;
}
return false;
}
}
}
答案 0 :(得分:0)
您应该在视图中复制它,而不是移动EditText。例如,您应该执行以下操作:
String editTextString = editText.getText().toString();
EditText newEditText = new EditText(MainActivity.this);
newEditText.setText(editTextString);
因此,您需要从EditText复制文本并创建一个新的EditText,您将添加到容器中。调用v.startDrag(data,shadowBuilder,v,0)时,请确保不要使视图不可见;