Ich habe einen Edittext, den ich von einem Layout in ein anderes kopieren wollte. Ich habe OnTouchListener implementiert. Die Probleme, auf die ich gestoßen bin, sind folgende;Android OnTouchListener verhindert die Rückrufmethode OnFocusChangeListener
- Die Callback-Methode OnFucusChangeListener wurde nicht ausgelöst.
- Der Editiertext wird nicht kopiert, sondern gezogen.
-Code
private final 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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
view.startDragAndDrop(data, shadowBuilder, view, 0);
}else{
view.startDrag(data, shadowBuilder, view, 0);
}
view.setVisibility(View.VISIBLE);
return true;
}else{
return false;
}
}
}
class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
//View vi = (View) event.getLocalState();
//vi.setOnTouchListener(new MyTouchListener());
default:
break;
}
return true;
}
}
Layout-
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:columnWidth="320dp"
android:orientation="horizontal"
android:rowCount="1"
android:stretchMode="columnWidth" >
<LinearLayout
android:id="@+id/topleft"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="320dp"
android:layout_row="0"
android:background="@drawable/drop_target" >
<EditText
android:id="@+id/etUserText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/enter_text"
android:inputType="textMultiLine"
android:clickable="true"
android:focusableInTouchMode="true"
android:focusable="true"/>
<Button
android:id="@+id/bAddImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_image"/>
</LinearLayout>
<LinearLayout
android:id="@+id/topright"
android:orientation="vertical"
android:layout_width="280dp"
android:layout_height="200dp"
android:background="@drawable/drop_target" >
<ImageView
android:id="@+id/iUserPicture"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/picture_holder"
android:scaleType="fitXY" />
</LinearLayout>
</GridLayout>
So kann ich die EditText ziehen, aber wenn ich es berühren, wird die Tastatur nicht angezeigt. Die Tastatur wird angezeigt, wenn ich sie unter Verwendung eines Emulators mit Lollipop teste. Ist das der richtige Ansatz?
Ich schätze Ihre Bemühungen sehr zu helfen. Leider hat deine Lösung bei mir nicht funktioniert. –