【问题标题】:How To Drag A TextView To EditText And Append TextView Text To EditText On Drop?如何将 TextView 拖到 EditText 并将 TextView 文本附加到 EditText 拖放?
【发布时间】:2020-12-07 07:59:58
【问题描述】:

我是 Android 新手,我的布局中有一个 TextViewEditText。我想将文本从 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>

提前致谢,小小帮助 非常感谢!

【问题讨论】:

  • 感谢安舒指正帖子

标签: java android textview android-edittext


【解决方案1】:

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


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;
        }

    });```

【讨论】:

    猜你喜欢
    • 2013-12-03
    • 1970-01-01
    • 2015-03-13
    • 2013-03-10
    • 2011-11-02
    • 1970-01-01
    • 2020-01-23
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多