0
我有一個edittext,我想從一個佈局複製到另一個。我實現了OnTouchListener。我遇到的問題如下:Android OnTouchListener可以防止OnFocusChangeListener回調方法
- OnFucusChangeListener回調方法未被觸發。
- 拖動edittext不會被複制。
代碼
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
view.startDragAndDrop(data, shadowBuilder, view, 0);
}else{
view.startDrag(data, shadowBuilder, view, 0);
}
view.setVisibility(View.VISIBLE);
return true;
}else{
return false;
}
}
}
class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
//View vi = (View) event.getLocalState();
//vi.setOnTouchListener(new MyTouchListener());
default:
break;
}
return true;
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:columnWidth="320dp"
android:orientation="horizontal"
android:rowCount="1"
android:stretchMode="columnWidth" >
<LinearLayout
android:id="@+id/topleft"
android:orientation="vertical"
android:layout_width="100dp"
android:layout_height="320dp"
android:layout_row="0"
android:background="@drawable/drop_target" >
<EditText
android:id="@+id/etUserText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/enter_text"
android:inputType="textMultiLine"
android:clickable="true"
android:focusableInTouchMode="true"
android:focusable="true"/>
<Button
android:id="@+id/bAddImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_image"/>
</LinearLayout>
<LinearLayout
android:id="@+id/topright"
android:orientation="vertical"
android:layout_width="280dp"
android:layout_height="200dp"
android:background="@drawable/drop_target" >
<ImageView
android:id="@+id/iUserPicture"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/picture_holder"
android:scaleType="fitXY" />
</LinearLayout>
</GridLayout>
所以我可以拖動的EditText但是當我觸摸它,不顯示鍵盤。當我使用運行Lollipop的模擬器測試它時,顯示鍵盤。這是正確的方法嗎?
我真的很感謝你的幫助。不幸的是,您的解決方案對我無效。 –