如何将textview拖到edittext并在拖放时将textview文本附加到edittext?

lh80um4z  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(337)

我是android新手,我有一个文本视图和编辑文本在我的布局。我想把文本从textview拖到edittext!需要帮助吗
我的xml文件:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="8dp"
            android:text="Hello World"
            android:textSize="12sp"
            android:textColor="#000000"/>
        <EditText
            android:id="@+id/edittext1"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:padding="8dp"
            android:textSize="12sp"
            android:textColor="#000000"
            android:hint="Edit Text"
            android:textColorHint="#607D8B"/>
    </LinearLayout>
</LinearLayout>

提前谢谢,我们将不胜感激!

mzillmmw

mzillmmw1#

我用下面的代码解决了我的问题:

String MyString = textview1.getText().toString();

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        ClipData data = ClipData.newPlainText("text", MyString);
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);

    }
        return true;    
  }

});

textview1.setOnDragListener(new OnDragListener(){

        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch(event.getAction())
            {
                case DragEvent.ACTION_DRAG_STARTED: break;
                case DragEvent.ACTION_DROP: ClipData.Item item = event.getClipData().getItemAt(0);
                                            CharSequence paste = item.getText();
                                            edittext1.append(paste.toString());                                         
                                            break;
                case DragEvent.ACTION_DRAG_ENDED: break;
                case DragEvent.ACTION_DRAG_EXITED: break;
                default: break;
            }

            return false;
        }

    });```

相关问题