我有一个edittext,我想从一个布局复制到另一个布局。我实现了OnTouchListener。我遇到的问题如下:
代码
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>
所以我可以拖动编辑文本,但是当我触摸它时,键盘不会显示。当我使用运行Lollipop的模拟器测试时,会显示键盘。这是正确的方法吗?
答案 0 :(得分:0)
Android一般只支持一个注册的侦听器。
所以我可以拖动编辑文本但是当我触摸它时,键盘不是 显示。
这种情况正在发生,因为 DragListener覆盖了TouchListener 。
你可以有一个解决方法,包括创建一个只使用复合模式将事件转发给其他监听器的监听器。
你可以这样做:
private class CustomCompositeOnClickListener implements View.OnClickListener {
List<View.OnClickListener> listeners;
public CustomCompositeOnClickListener(){
listeners = new ArrayList<View.OnClickListener>();
}
public void addOnClickListener(View.OnClickListener listener){
listeners.add(listener);
}
@Override
public void onClick(View v){
for(View.OnClickListener listener : listeners){
listener.onClick(v);
}
}
}
设置按钮(或可点击的视图),如下所示:
CustomCompositeOnClickListener groupListener = new CustomCompositeOnClickListener();
Button button = (Butto) findViewById(R.id.button);
button.setOnClickListener(groupListener);
每当您想要添加另一个侦听器时,请将其添加到groupListener
groupListener.addOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// implement your behaviour here
}
});
此致