如何在 Android 中使用拖放?
本例说明了如何在 Android 中使用拖放。
步骤 1 − 在 Android Studio 中创建一个新项目,在“文件 ⇒ 新建项目”中填写所有必需的详细信息来创建一个新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml 中。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="MainActivity" > <LinearLayout android:id="@+id/leftView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:background="@android:color/darker_gray" android:gravity="center_vertical" android:orientation="vertical" > <ImageView android:id="@+id/boxView" android:layout_width="75dp" android:layout_height="75dp" android:layout_gravity="center_vertical|center_horizontal" android:layout_margin="10dp" android:background="@drawable/one" /> </LinearLayout> <LinearLayout android:id="@+id/rightView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" android:background="@android:color/darker_gray" android:gravity="center_vertical" android:orientation="vertical" > </LinearLayout> </LinearLayout>
步骤 3 − 将以下代码添加到 src/MainActivity.java 中
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnDragListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.boxView).setOnTouchListener(this);
findViewById(R.id.leftView).setOnDragListener(this);
findViewById(R.id.rightView).setOnDragListener(this);
}
@Override
public boolean onDrag(View view, DragEvent event) {
if (event.getAction() == DragEvent.ACTION_DROP) {
view = (View) event.getLocalState();
if (view.getId() == R.id.leftView || view.getId() == R.id.rightView) {
ViewGroup source = (ViewGroup) view.getParent();
source.removeView(view);
LinearLayout target = (LinearLayout) view;
target.addView(view);
}
view.setVisibility(View.VISIBLE);
}
return true;
}
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
}
return false;
}
}步骤 4 - 将以下代码添加到 androidManifest.xml 中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行你的应用程序。我假设你已将实际的 Android 移动设备连接到你的计算机。如需从 Android Studio 运行应用程序,请打开其中一个项目的活动文件并单击工具栏中的“运行
”图标。选择你的移动设备作为选项,然后检查你的移动设备,其将显示你的默认屏幕 -

点击 此处 下载项目代码。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP