Ich möchte TextView
auf die ImageView
verschieben, aber ich kann nicht verschieben, obwohl ich den Code verwenden, der auf der Google Developer-Seite ist. Wie kann ich eine TextView
überall auf dem Bildschirm in Android ziehen und ablegen?Drag and Drop TextView überall
Antwort
XML-Datei:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Test1" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Test2" />
</RelativeLayout>
Java-Datei
public class TestActivity extends Activity {
int pressed_x,pressed_y,pressed_x1,pressed_y1;
TextView tv1,tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv1.setOnTouchListener(mOnTouchListenerTv1);
tv2.setOnTouchListener(mOnTouchListenerTv2);
}
public final View.OnTouchListener mOnTouchListenerTv1 = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
RelativeLayout.LayoutParams relativeLayoutParams = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
Log.d("TAG","@@@@ TV1 ACTION_UP");
// Where the user started the drag
pressed_x = (int) event.getRawX();
pressed_y = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG","@@@@ TV1 ACTION_UP");
// Where the user's finger is during the drag
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
// Calculate change in x and change in y
int dx = x - pressed_x;
int dy = y - pressed_y;
// Update the margins
relativeLayoutParams.leftMargin += dx;
relativeLayoutParams.topMargin += dy;
tv1.setLayoutParams(relativeLayoutParams);
// Save where the user's finger was for the next ACTION_MOVE
pressed_x = x;
pressed_y = y;
break;
case MotionEvent.ACTION_UP:
Log.d("TAG","@@@@ TV1 ACTION_UP");
break;
}
return true;
}
};
public final View.OnTouchListener mOnTouchListenerTv2 = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
RelativeLayout.LayoutParams relativeLayoutParams1 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
Log.d("TAG","@@@@ TV2 ACTION_DOWN");
// Where the user started the drag
pressed_x1 = (int) event.getRawX();
pressed_y1 = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG","@@@@ TV2 ACTION_MOVE");
// Where the user's finger is during the drag
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
// Calculate change in x and change in y
int dx = x - pressed_x1;
int dy = y - pressed_y1;
// Update the margins
relativeLayoutParams1.leftMargin += dx;
relativeLayoutParams1.topMargin += dy;
tv2.setLayoutParams(relativeLayoutParams1);
// Save where the user's finger was for the next ACTION_MOVE
pressed_x1 = x;
pressed_y1 = y;
break;
case MotionEvent.ACTION_UP:
Log.d("TAG","@@@@ TV2 ACTION_UP");
break;
}
return true;
}
};
}
Es funktioniert, wenn ich einzelne TextView, aber es funktioniert nicht, wenn ich mehr als ein TextView (Wenn ich tv1 nach dem Verschieben oder berühren Berühren von tv2, jedes Mal, wenn tv1 sich bewegt, obwohl ich versuche, tv2 zu bewegen). –
Fügen Sie Ihren aktualisierten Code – user2025187
Ich fügte meinen Code unter –
XML-Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Test1" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_centerInParent="true"
android:text="Test2" />
</RelativeLayout>
Java-Code:
public class MainActivity extends Activity {
int pressed_x,pressed_y,pressed_x1,pressed_y1;
TextView tv1,tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv1.setOnTouchListener(mOnTouchListenerTv1);
tv2.setOnTouchListener(mOnTouchListenerTv2);
}
public final View.OnTouchListener mOnTouchListenerTv1 = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
RelativeLayout.LayoutParams relativeLayoutParams = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
Log.d("TAG","@@@@ TV1 ACTION_UP");
// Where the user started the drag
pressed_x = (int) event.getRawX();
pressed_y = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG","@@@@ TV1 ACTION_UP");
// Where the user's finger is during the drag
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
// Calculate change in x and change in y
int dx = x - pressed_x;
int dy = y - pressed_y;
// Update the margins
relativeLayoutParams.leftMargin += dx;
relativeLayoutParams.topMargin += dy;
tv1.setLayoutParams(relativeLayoutParams);
// Save where the user's finger was for the next ACTION_MOVE
pressed_x = x;
pressed_y = y;
break;
case MotionEvent.ACTION_UP:
Log.d("TAG","@@@@ TV1 ACTION_UP");
break;
}
return true;
}
};
public final View.OnTouchListener mOnTouchListenerTv2 = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
RelativeLayout.LayoutParams relativeLayoutParams1 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
Log.d("TAG","@@@@ TV2 ACTION_DOWN");
// Where the user started the drag
pressed_x1 = (int) event.getRawX();
pressed_y1 = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG","@@@@ TV2 ACTION_MOVE");
// Where the user's finger is during the drag
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
// Calculate change in x and change in y
int dx = x - pressed_x1;
int dy = y - pressed_y1;
// Update the margins
relativeLayoutParams1.leftMargin += dx;
relativeLayoutParams1.topMargin += dy;
tv2.setLayoutParams(relativeLayoutParams1);
// Save where the user's finger was for the next ACTION_MOVE
pressed_x1 = x;
pressed_y1 = y;
break;
case MotionEvent.ACTION_UP:
Log.d("TAG","@@@@ TV2 ACTION_UP");
break;
}
return true;
}
};
}
- 1. eckig2 drag and drop
- 2. Drag and Drop Sortierbar
- 3. PyQt4 - Drag and Drop
- 4. Java Drag and drop
- 5. Android Drag and Drop Implementierung
- 6. Qt :: Drag and Drop Bild
- 7. Python Drag-and-Drop gebrochen
- 8. drag and drop mit raphael
- 9. Drag-and-Drop Web-Framework
- 10. Drag and Drop jquery UI
- 11. Drag and Drop eine Taste?
- 12. Drag-and-Drop-Datei Hochladen
- 13. Einfache Drag-and-Drop-Implementierung
- 14. JavaScript Drag-and-Drop-Seitenerstellung
- 15. Android FAB drag and drop
- 16. Java Drag and Drop auf native os
- 17. jquery drag and drop in IE
- 18. Drag and Drop Icons Home Screen
- 19. Drag-and-Drop-Dateien in HTML-Eingabe
- 20. pyside qtreewidget Constrain drag and drop
- 21. GWT-DnD (drag and drop) Layout Frage
- 22. jsTree drag and drop funktioniert nicht immer
- 23. Delphi - Drag and Drop + MouseDown- + MouseUp
- 24. NSTableView und NSOutlineView Drag-and-Drop
- 25. Jquery Drag-and-Drop-Eindämmungsproblem - CSS-Problem?
- 26. Javascript/CSS Drag-and-Drop Hochladen
- 27. Drag and Drop Halten der Header-Teil
- 28. Drag-and-drop Cross-Domains, iFrames, Browserfenster
- 29. Jquery Drag and Drop zum Arbitrieren Ort
- 30. Datei-Upload (Drag and Drop) funktioniert nicht
Fügen Sie den Code dessen hinzu, was Sie bereits ausprobiert haben - auch wenn Sie beim Versuch, TextView zu verschieben, Fehler erhalten, schließen Sie diese auch in Ihre Frage ein, damit die Community helfen kann; In der [Quick Tour of StackOverflow] (http://stackoverflow.com/help) finden Sie Tipps, wie Sie gute Fragen stellen können. – ishmaelMakitla
Der Code auf der Google Developer Page funktioniert ... Die einzige Möglichkeit, uns zu helfen, ist um Ihren Code zu sehen und zu erklären, was Sie anders machen – Entreco
Nein, es funktioniert nicht .. Es enthält Fehler –