android - 编辑android时可拖动的Textview文本超出屏幕范围
问题描述
我有一个文本视图,它可以拖动到屏幕的任何点,它工作正常,并且这个可拖动的条件是视图只能在屏幕范围内移动。现在,我将文本视图移动到屏幕的最右端并尝试从编辑文本中编辑文本视图,但我遇到了一个问题,即文本视图超出了屏幕的范围希望它在屏幕范围内,如果文本增加,则应在屏幕范围内调整文本。我已经尝试过 AutoResizeTextView 但我不想调整文本的大小,而是根据文本调整容器的大小,比如如果文本增加它应该调整它的宽度。
这是我的代码:
class MainActivity : AppCompatActivity() {
var etTxt: EditText? = null
var tvTxt: TextView? = null
var lisDrag: OnDragTouchListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
etTxt = findViewById(R.id.etTxt)
tvTxt = findViewById(R.id.tvTxt)
lisDrag = OnDragTouchListener(tvTxt)
tvTxt?.setOnTouchListener(lisDrag)
etTxt?.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
tvTxt?.setText(s.toString())
}
})
}
}
public class OnDragTouchListener implements View.OnTouchListener {
/**
* Callback used to indicate when the drag is finished
*/
public interface OnDragActionListener {
/**
* Called when drag event is started
*
* @param view The view dragged
*/
void onDragStart(View view);
/**
* Called when drag event is completed
*
* @param view The view dragged
*/
void onDragEnd(View view);
}
private View mView;
private View mParent;
private boolean isDragging;
private boolean isInitialized = false;
private int width;
private float xWhenAttached;
private float maxLeft;
private float maxRight;
private float dX;
private int height;
private float yWhenAttached;
private float maxTop;
private float maxBottom;
private float dY;
private OnDragActionListener mOnDragActionListener;
public OnDragTouchListener(View view) {
this(view, (View) view.getParent(), null);
}
public OnDragTouchListener(View view, View parent) {
this(view, parent, null);
}
public OnDragTouchListener(View view, OnDragActionListener onDragActionListener) {
this(view, (View) view.getParent(), onDragActionListener);
}
public OnDragTouchListener(View view, View parent, OnDragActionListener onDragActionListener) {
initListener(view, parent);
setOnDragActionListener(onDragActionListener);
}
public void setOnDragActionListener(OnDragActionListener onDragActionListener) {
mOnDragActionListener = onDragActionListener;
}
public void initListener(View view, View parent) {
mView = view;
mParent = parent;
isDragging = false;
isInitialized = false;
}
public void updateBounds() {
updateViewBounds();
updateParentBounds();
isInitialized = true;
}
public void updateViewBounds() {
width = mView.getWidth();
xWhenAttached = mView.getX();
dX = 0;
height = mView.getHeight();
yWhenAttached = mView.getY();
dY = 0;
}
public void updateParentBounds() {
maxLeft = 0;
maxRight = maxLeft + mParent.getWidth();
maxTop = 0;
maxBottom = maxTop + mParent.getHeight();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (isDragging) {
float[] bounds = new float[4];
// LEFT
bounds[0] = event.getRawX() + dX;
if (bounds[0] < maxLeft) {
bounds[0] = maxLeft;
}
// RIGHT
bounds[2] = bounds[0] + width;
if (bounds[2] > maxRight) {
bounds[2] = maxRight;
bounds[0] = bounds[2] - width;
}
// TOP
bounds[1] = event.getRawY() + dY;
if (bounds[1] < maxTop) {
bounds[1] = maxTop;
}
// BOTTOM
bounds[3] = bounds[1] + height;
if (bounds[3] > maxBottom) {
bounds[3] = maxBottom;
bounds[1] = bounds[3] - height;
}
switch (event.getAction()) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
onDragFinish();
break;
case MotionEvent.ACTION_MOVE:
mView.animate().x(bounds[0]).y(bounds[1]).setDuration(0).start();
break;
}
return true;
} else {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isDragging = true;
if (!isInitialized) {
updateBounds();
}
dX = v.getX() - event.getRawX();
dY = v.getY() - event.getRawY();
if (mOnDragActionListener != null) {
mOnDragActionListener.onDragStart(mView);
}
return true;
}
}
return false;
}
private void onDragFinish() {
if (mOnDragActionListener != null) {
mOnDragActionListener.onDragEnd(mView);
}
dX = 0;
dY = 0;
isDragging = false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#fff"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<com.elyeproj.drawtext.AutoResizeTextView
android:id="@+id/tvTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I am the draggable text"
android:textColor="@color/colorPrimary"
android:textSize="16dp" />
</LinearLayout>
<EditText
android:id="@+id/etTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="12dp" />
</LinearLayout>
</RelativeLayout>
解决方案
推荐阅读
- github - Github 上的 Markdown 意外行为
- javascript - Uncaught (in promise) ReferenceError: topojson is not defined
- reactjs - 如何在 IonButton 中将 href 与 onClick() 一起使用?
- python - 绘制一系列二进制数组
- python - 相当于 defusedxml 中的 lxml.objectify cleanup_namespaces
- node.js - 使用 supertest 在快速服务器上开玩笑超时
- python - 如何使用 Python exchangelib v 4.5.1 过滤“作者”或“发件人”?
- r - DataExplorer,自定义单变量分布
- powerquery - 如何按天“合并”具有专有员工姓名的日期表
- heroku - HTTPS 与 HTTP 和 IoT 设备交互