Posted at

kotlinでandroidのtextdrag

More than 1 year has passed since last update.


概要

TextViewを長押しでドラッグする


MainActivity.kt

import android.os.Build

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.view.View

class MainActivity : AppCompatActivity() , View.OnLongClickListener {
lateinit var vi : View
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

vi = findViewById(R.id.text)
vi.setOnLongClickListener(this)
}
@RequiresApi(Build.VERSION_CODES.N)
override fun onLongClick(v : View): Boolean {
v.startDrag(null, View.DragShadowBuilder(v), v, 2)
return true;
}
}



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.nimani.drag.MainActivity">

<TextView
android:id="@+id/text"
android:layout_width="121dp"
android:layout_height="66dp"
android:text="Hello World!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="109dp" />

</android.support.constraint.ConstraintLayout>


それだけ