세 가지 레이아웃 : 상단, 왼쪽 및 오른쪽. 맨 위 레이아웃에서 EditText 아이콘이 있습니다. 왼쪽 레이아웃으로 드래그하면 왼쪽 레이아웃에 EditText가 생성됩니다.드래그 앤 드롭으로 EditText 만들기 Android 문제
: 왼쪽 레이아웃 또는 오른쪽 레이아웃 글고 드롭으로, 내가 바로 레이아웃 왼쪽에서 이동할 수 있습니다
내 문제를 (왼쪽 또는 오른쪽으로, 맨 왼쪽/오른쪽에서 이동할 수 없습니다)
1. 왼쪽 레이아웃에서 드래그하여 EditText를 만들면 왼쪽 레이아웃에서 드래그하여 EditText를 만듭니다. 모든 드래그 드롭 로그 동작을 볼 수 있습니다. 그러나 왼쪽 레이아웃에 또 다른 EditText를 다시 작성하면 ACTION_DRAG_STARTED 및 ACTION_DRAG_ENDED가 수신되지 않습니다. 이미 레이아웃에서 생성 된 글고 너무 가까이 글고을 만들
할수 있도록 팝업
http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at60954PM_zps14a2d784.png는 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>
활동 : package com.trandu ctrinh.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;
}
}
}
아직 작동하지 않는 문제가 2 개 있습니다. – tranductrinh