安卓TextView 拖动命名

需求:该布局文件使用线性布局来排列三个文本视图和一个按钮,分别用于显示两个动物名称以及占位文本视图。在占位文本视图中,我们为其设置了背景和居中显示样式,并用其作为接收拖放操作的目标

效果图;

实现代码

第一布局文件

XML 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/animalName1"
        android:text="狮子"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/animalName2"
        android:text="老虎"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/namePlaceholder"
        android:text="请将动物名字拖到这里"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background_normal"
        android:gravity="center"/>

    <Button
        android:id="@+id/btnGetTexts"
        android:text="获取文本"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

实现代码

java 复制代码
public class AttentionQuestionsActivity extends AppCompatActivity {

    // 用于存储动物名称视图和其对应的名称字符串
    private Map<View, String> nameMap = new HashMap<>();
    // 存储被拖放的文本字符串
    private ArrayList<String> draggedTexts = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attention_questions);

        // 获取 UI 控件
        TextView animalName1 = findViewById(R.id.animalName1);
        TextView animalName2 = findViewById(R.id.animalName2);
        TextView namePlaceholder = findViewById(R.id.namePlaceholder);
        Button btnGetTexts = findViewById(R.id.btnGetTexts);

        // 将动物名称视图和其对应的字符串存储到 nameMap 中
        nameMap.put(animalName1, "狮子");
        nameMap.put(animalName2, "老虎");

        // 为每个动物名称视图设置长按监听器
        for (View view : nameMap.keySet()) {
            // 将名称字符串存储到视图的 tag 中
            view.setTag(nameMap.get(view));
            view.setOnLongClickListener(v -> {
                // 创建 ClipData 对象并启动拖放操作
                ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
                String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
                ClipData data = new ClipData(v.getTag().toString(), mimeTypes, item);

                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                v.startDragAndDrop(data, shadowBuilder, null, 0);
                return true;
            });
        }

        // 设置占位文本视图的拖放监听器
        namePlaceholder.setOnDragListener((v, event) -> {
            switch (event.getAction()) {
                case DragEvent.ACTION_DROP:
                    // 获取被拖放的文本并设置到占位文本视图中
                    ClipData.Item item = event.getClipData().getItemAt(0);
                    CharSequence name = item.getText();
                    ((TextView) v).setText(name);
                    draggedTexts.add(name.toString());
                    break;
            }
            return true;
        });

        // 设置获取文本按钮的点击监听器
        btnGetTexts.setOnClickListener(v -> {
            // 将被拖放的文本字符串拼接成一个长字符串并显示到 Toast 中
            StringBuilder sb = new StringBuilder();
            for (String text : draggedTexts) {
                sb.append(text).append("\n");
            }
            Toast.makeText(AttentionQuestionsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
        });
    }
}

注意

在这个类中,我们为每个动物名称视图设置了长按监听器,并在其中启动了拖放操作。同时,我们还为占位文本视图设置了拖放监听器,并在其中将被拖放的文本设置到该视图中。最后,当用户点击获取文本按钮时,我们将被拖放的文本字符串拼接成一个长字符串并显示到 Toast 中。以上只是简单功能实现,

相关推荐
7177777 小时前
国内 AI 研发效能工具怎么选:2026 年基于公开信息的选型框架与 Gitee 实践
人工智能·gitee
雨白9 小时前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
hai_android13 小时前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m14 小时前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui14 小时前
没人认领 .md:安卓把报告交给了音乐播放器打开
android
用户607097591490617 小时前
AOSP 14 OTA 升级指导(四):update_engine 内部机制——Action 流水线与写盘
android
恋猫de小郭21 小时前
Jetpack Compose 1.13 发布,憋了这么久终有交代了
android·前端·flutter
JMchen12321 小时前
【Android 性能优化实战 60 讲】12 异步 Inflate 实战与痛击:源码拆解原生瓶颈,手写增强版布局预加载框架
android·性能优化·实战·启动优化·异步inflate·x2c
好的佩奇1 天前
蓝牙之设备角色与连接
android·flutter·蓝牙
雨白2 天前
Android 消息机制剖析:从 Handler 投递到 Looper 循环的全链路
android