gpt4 book ai didi

android - 我想在将一个 View 拖放到另一个 View 后开始新的 Activity。在 switch case 中我该怎么做?

转载 作者:行者123 更新时间:2023-11-30 01:20:34 24 4
gpt4 key购买 nike

我已经搜索了一个多星期,但我无法弄清楚如何从案例中开始新 Activity 。

这是来自 android studio 的示例拖放示例代码。我该如何添加?...我想创建一个场景,如果我将一个 View 拖到另一个 View 中,则会启动一个新的 Activity 页面。这是我在互联网上的第一个问题。

我想放弃并且感到沮丧,因为我有一份新手工作机会,除非我这样做,否则我无法接受。

public class DragActivity extends Activity {

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag);
findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
findViewById(R.id.topright).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());

}

private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}

class MyDragListener implements OnDragListener {
Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
Drawable normalShape = getResources().getDrawable(R.drawable.shape);

@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:
v.setBackgroundDrawable(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundDrawable(normalShape);
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:
v.setBackgroundDrawable(normalShape);

default:
break;
}
return true;
}

}
}

最佳答案

我创建了两个 ImageView (红色和灰色),当拖动红色 View 并放入灰色时,会引发一个新的 Activity。

主 Activity

public class MainActivity extends AppCompatActivity implements View.OnDragListener,
View.OnTouchListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView left = (ImageView) findViewById(R.id.left);
ImageView right = (ImageView) findViewById(R.id.right);

left.setOnTouchListener(this);
left.setOnDragListener(this);
right.setOnDragListener(this); // Added a drag listener to the other view

}

@Override
public boolean onDrag(View v, DragEvent event) {

int action = event.getAction();
switch (action) {
case ACTION_DROP: {
/**
* If the item view is the origin return false, reject the drop event
*/
if (v.getId() == R.id.left)
return false;
/**
* Else accept the event and raise the activity
*/
startActivity(new Intent(MainActivity.this, DragDropActivity.class));
}
}
return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {

if (v.getId() == R.id.right) return false;

if (event.getAction() == MotionEvent.ACTION_DOWN) {
//setup drag
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
return true;
}
return false;
}
}

activity_main.xml:

<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="forkandjoin.draganddrop.MainActivity">

<ImageView
android:id="@+id/left"
android:layout_width="50dip"
android:layout_height="50dip"
android:background="#FF0000"
android:layout_alignParentLeft="true"
/>

<ImageView
android:id="@+id/right"
android:layout_width="50dip"
android:layout_height="50dip"
android:background="#929292"
android:layout_alignParentRight="true"/>

</RelativeLayout>

应用

enter image description here

当您将红色项目放入灰色项目时,会引发一个新的 Activity 。

好好享受吧。

关于android - 我想在将一个 View 拖放到另一个 View 后开始新的 Activity。在 switch case 中我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37176721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com