android - 拖放后如何交换2个按钮?

标签 android button drag-and-drop listener

我正在尝试创建某种内存应用程序来学习不同语言的单词。

到目前为止,我已经使用 TextViewButton 创建了布局,并且按钮是可拖动的 - 可以长按和拖动。

现在我想让当你将一个按钮拖到另一个按钮上时交换按钮成为可能,但我注意到到目前为止按钮不能有 onDragListener 因为我收到以下错误:

The method setOnDragListener(View.OnDragListener) in the type View is not applicable for the arguments (new OnDragListener(){})

到目前为止我的代码是这样的

for (final Button b : buttonList) {
  //b.setOnDragListener(new MyDragListener());

  b.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
       ClipData.Item item = new ClipData.Item( ((Button) v).getText() );

       String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
       ClipData dragData = new ClipData(
           ((Button) v).getText().toString(), 
           mimeTypes, 
           item);

       // Instantiates the drag shadow builder.
       View.DragShadowBuilder myShadow = new DragShadowBuilder(b);

       // Starts the drag
       v.startDrag(dragData,  // the data to be dragged
                   myShadow,  // the drag shadow builder
                   null,      // no need to use local data
                   0          // flags (not currently used, set to 0)
       );
       return true;
    }
 });

  // Create and set the drag event listener for the View
  b.setOnDragListener( new OnDragListener(){
     // ....
  }
}

最佳答案

您缺少导入语句。

import View.OnDragListener;

.setOnDragListener(new View.OnDragListener()

关于android - 拖放后如何交换2个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23499560/

相关文章:

android - GridView onScrollPositionChanged?

java - 如何在Android中的firestore中存储bigdecimal

javascript - HTML5 button.formaction 属性在 <form> 之外不起作用

android - 如何按升序或降序排列firebase数据库数据?

android - 在 Android 的按钮内居中大文本不起作用

PHP: onClick 不工作

javascript - 阻止 PDF 文件在拖放时打开

jquery - 获取我拖动元素的div的id

php - 在 HTML5 中拖放多个文件?

java - 制作一个自动跳线的布局表