- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 gridView,我想根据单击的项目的位置启动不同的 Intent 。
我已经实例化了以下 onClick 监听器,其中包含位置值:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
但我相信我需要在其中添加一个 if 语句来确定根据此位置启动哪个 Intent 。到目前为止,我已经提出了以下内容,但我不相信这正是我正在寻找的。
if position = 1 { Intent intent = new Intent(this, Activity1.class); }else if position = 2 { Intent intent = new Intent(this, Activity1.class); }
非常感谢任何建议。
/** * 此 Activity 呈现一个带有网格的屏幕,可以在上面添加图像并 * 四处走动。它还定义了屏幕上拖动 View 可以的区域 * 被丢弃。当对象被拖过时向用户提供反馈 * 这些放置区。 * *
* 与上一版本的DragView示例中的DragActivity类似 * application,这里的代码源自Android Launcher代码。 * *
* 原始启动器代码需要长按(按)才能启动 * 拖放序列。如果您想看到该行为,请设置变量 * mLongClickStartsDrag 为 true。下面将其设置为 false,这意味着任何 * 触摸事件启动拖放。 * */
public class DragActivity extends Activity implements View.OnLongClickListener,
View.OnClickListener, View.OnTouchListener // ,
// AdapterView.OnItemClickListener
{
/**
*/
// Constants
private static final int HIDE_TRASHCAN_MENU_ID = Menu.FIRST;
private static final int SHOW_TRASHCAN_MENU_ID = Menu.FIRST + 1;
private static final int ADD_OBJECT_MENU_ID = Menu.FIRST + 2;
private static final int CHANGE_TOUCH_MODE_MENU_ID = Menu.FIRST + 3;
private boolean isErase = true;
private EditText et;
private TextView tx;
/**
*/
// Variables
private DragController mDragController; // Object that handles a drag-drop
// sequence. It intersacts with
// DragSource and DropTarget
// objects.
private DragLayer mDragLayer; // The ViewGroup within which an object can be
// dragged.
private DeleteZone mDeleteZone; // A drop target that is used to remove
// objects from the screen.
private int mImageCount = 0; // The number of images that have been added to
// screen.
private ImageCell mLastNewCell = null; // The last ImageCell added to the
// screen when Add Image is clicked.
private boolean mLongClickStartsDrag = true; // If true, it takes a long
// click to start the drag
// operation.
// Otherwise, any touch
// event starts a drag.
public static final boolean Debugging = false; // Use this to see extra
// toast messages.
/**
*/
// Methods
/**
* Add a new image so the user can move it around. It shows up in the
* image_source_frame part of the screen.
*
* @param resourceId
* int - the resource id of the image to be added
*/
public void addNewImageToScreen(int resourceId) {
if (mLastNewCell != null)
mLastNewCell.setVisibility(View.GONE);
FrameLayout imageHolder = (FrameLayout) findViewById(R.id.image_source_frame);
if (imageHolder != null) {
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
Gravity.CENTER);
ImageCell newView = new ImageCell(this);
newView.setImageResource(resourceId);
imageHolder.addView(newView, lp);
newView.mEmpty = false;
newView.mCellNumber = -1;
mLastNewCell = newView;
mImageCount++;
// Have this activity listen to touch and click events for the view.
newView.setOnClickListener(this);
newView.setOnLongClickListener(this);
newView.setOnTouchListener(this);
}
}
/**
* Add one of the images to the screen so the user has a new image to move
* around. See addImageToScreen.
*
*/
public void addNewImageToScreen() {
int resourceId = R.drawable.sqwhite;
addNewTextToScreen();
int m = mImageCount % 3;
if (m == 1)
resourceId = R.drawable.sqdrk;
else if (m == 2)
resourceId = R.drawable.sqwhite;
addNewImageToScreen(resourceId);
}
private void addNewTextToScreen() {
// TODO Auto-generated method stub
// et.setVisibility(View.VISIBLE);
if (isErase) {
tx.setText(et.getText().toString());
} else {
tx.setText("");
et.setVisibility(View.GONE);
}
isErase = !isErase;
}
/**
* Handle a click on a view.
*
*/
public void onClick(View v) {
if (mLongClickStartsDrag) {
// Tell the user that it takes a long click to start dragging.
//toast("Press and hold to drag an image.");
}
}
/**
* Handle a click of the Add Image button
*
*/
public void onClickAddImage(View v) {
addNewImageToScreen();
}
/**
* onCreate - called when the activity is first created.
*
* Creates a drag controller and sets up three views so click and long click
* on the views are sent to this activity. The onLongClick method starts a
* drag sequence.
*
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
et = (EditText) findViewById(R.id.editText1);
et.setVisibility(View.INVISIBLE);
tx = (TextView) findViewById(R.id.textView1);
GridView gridView = (GridView) findViewById(R.id.image_grid_view);
if (gridView == null)
toast("Unable to find GridView");
else {
gridView.setAdapter(new ImageCellAdapter(this));
// gridView.setOnItemClickListener (this);
}
mDragController = new DragController(this);
mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
mDragLayer.setDragController(mDragController);
mDragLayer.setGridView(gridView);
mDragController.setDragListener(mDragLayer);
// mDragController.addDropTarget (mDragLayer);
mDeleteZone = (DeleteZone) findViewById(R.id.delete_zone_view);
// Give the user a little guidance.
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.instructions),
Toast.LENGTH_LONG).show();
}
/**
* Build a menu for the activity.
*
*/
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, HIDE_TRASHCAN_MENU_ID, 0, "Hide Trashcan").setShortcut('1',
'c');
menu.add(0, SHOW_TRASHCAN_MENU_ID, 0, "Show Trashcan").setShortcut('2',
'c');
menu.add(0, ADD_OBJECT_MENU_ID, 0, "Add View").setShortcut('9', 'z');
menu.add(0, CHANGE_TOUCH_MODE_MENU_ID, 0, "Change Touch Mode");
return true;
}
/**
* Handle a click of an item in the grid of cells.
*
*/
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageCell i = (ImageCell) v;
trace("onItemClick in view: " + i.mCellNumber);
}
/**
* Handle a long click. If mLongClick only is true, this will be the only
* way to start a drag operation.
*
* @param v
* View
* @return boolean - true indicates that the event was handled
*/
public boolean onLongClick(View v) {
if (mLongClickStartsDrag) {
// trace ("onLongClick in view: " + v + " touchMode: " +
// v.isInTouchMode ());
// Make sure the drag was started by a long press as opposed to a
// long click.
// (Note: I got this from the Workspace object in the Android
// Launcher code.
// I think it is here to ensure that the device is still in touch
// mode as we start the drag operation.)
if (!v.isInTouchMode()) {
toast("isInTouchMode returned false. Try touching the view again.");
return false;
}
return startDrag(v);
}
// If we get here, return false to indicate that we have not taken care
// of the event.
return false;
}
/**
* Perform an action in response to a menu item being clicked.
*
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case HIDE_TRASHCAN_MENU_ID:
if (mDeleteZone != null)
mDeleteZone.setVisibility(View.INVISIBLE);
return true;
case SHOW_TRASHCAN_MENU_ID:
if (mDeleteZone != null)
mDeleteZone.setVisibility(View.VISIBLE);
return true;
case ADD_OBJECT_MENU_ID:
// Add a new object to the screen;
addNewImageToScreen();
return true;
case CHANGE_TOUCH_MODE_MENU_ID:
mLongClickStartsDrag = !mLongClickStartsDrag;
String message = mLongClickStartsDrag ? "Changed touch mode. Drag now starts on long touch (click)."
: "Changed touch mode. Drag now starts on touch (click).";
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
.show();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* This is the starting point for a drag operation if mLongClickStartsDrag
* is false. It looks for the down event that gets generated when a user
* touches the screen. Only that initiates the drag-drop sequence.
*
*/
public boolean onTouch(View v, MotionEvent ev) {
// If we are configured to start only on a long click, we are not going
// to handle any events here.
if (mLongClickStartsDrag)
return false;
boolean handledHere = false;
final int action = ev.getAction();
// In the situation where a long click is not needed to initiate a drag,
// simply start on the down event.
if (action == MotionEvent.ACTION_DOWN) {
handledHere = startDrag(v);
}
return handledHere;
}
/**
* Start dragging a view.
*
*/
public boolean startDrag(View v) {
DragSource dragSource = (DragSource) v;
// We are starting a drag. Let the DragController handle it.
mDragController.startDrag(v, dragSource, dragSource,
DragController.DRAG_ACTION_MOVE);
return true;
}
/**
* Show a string on the screen via Toast.
*
* @param msg
* String
* @return void
*/
public void toast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
} // end toast
/**
* Send a message to the debug log. Also display it using Toast if Debugging
* is true.
*/
public void trace(String msg) {
Log.d("DragActivity", msg);
if (!Debugging)
return;
toast(msg);
}
} // end class
最佳答案
来自 GridView 的 android 文档:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
编辑:看起来 addNewImageToScreen() 是您添加 ImageCell 的地方,因此假设您可以在该范围内生成 Intent ..
Intent intent = new Intent(this, Activity1.class); // or whatever you want to run
ImageCell newView = ...
newView.setTag( intent );
然后在你的 onItemClick 中: 公共(public)无效onItemClick(AdapterView父级, View v,int位置,长id){ Toast.makeText(HelloGridView.this, ""+ 位置, Toast.LENGTH_SHORT).show(); Intent intent = (Intent) v.getTag(); //现在你可以根据你的 Intent 启动Activity.. }
关于java - 将 onClick 监听器添加到 gridView 项目(根据位置启动独特的 Intent ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20982317/
如果我想为收藏创建一个 Intent 。 如果用户问“他最喜欢什么”,它会显示一些建议芯片 因此它会调用与该芯片相关的任何后续 Intent 。 最喜欢的饮料 最喜欢的食物 最喜欢的电影等 我还想直接
我确信有一些显而易见的事情,但还没有找到解决这个简单问题的方法。错误是在用户猜出正确答案时尝试启动另一个 Activity 的主要 Activity : Error:(85, 23) Unresolv
public class MainActivity extends Activity { Button b; //FrameLayout fl; @Override p
我对 intentService 有点困惑。文档说,如果您向 intentService 发送多个任务( Intent ),那么它将在一个单独的线程上一个接一个地执行它们。我的问题是 - 是否可以同时
我正在尝试从其他应用程序获取 mime 类型 text/plain 的 Intent 并将该文本存储在字符串类型的变量中。它在 onCreate 方法中工作正常,但是当我使用 singleTask 作
我想知道,2个代码有什么区别? newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
如何设置我的 Activity 以响应任何类型的共享 Intent 。 我试过:- 但是这不起作用,我已经阅读了http://developer.android.co
鉴于子类具有不同的上下文以及在单击监听器后启动的不同 Activity ,父类(super class)中的代码 Intent Intent=new Intent(context,Activity.c
更新#1:更多信息添加到这篇文章的末尾 我是 Android 开发和测试的新手。 我有 3 个 Espresso 测试。第一个测试通过,但第二个不会运行,因为在第二个测试之前调用 setUp() 方法
我是 Espresso UI 测试的新手。 我在运行测试时遇到这个错误(ADT Eclipse IDE)。 该应用程序已经开发完成,并且在启动该应用程序时有很多请求正在进行。无法重写应用程序。但我需要
因此,尝试创建一个我认为是基本简历应用程序的应用程序。我有两个类(class),都有同样的问题。它说它“无法解析符号 Intent ” 谷歌部分做了,但没有任何意义.. 这是我的代码。 MainAct
我正在尝试将 user_id 值从一个 Intent 传递到另一个 Intent。我知道这是一个非常简单的过程,而且我已经这样做了好几次了。但对于下面的代码,我有点困惑。 我需要将 user_id 值
这是我将值传递给名为 choice 的类的主要 Activity 。 @Override public void onClick(View v) { // TODO Auto-generated me
我正在寻找一个 Android Intent 来翻译文本,我发现了这个: Google Translate Activity not working anymore 但我想在任务管理器中使用它。我真的
可以设置多个启动 Intent ,例如,当用户点击通知时。 让我解释一下我的具体问题: 我有一个带通知的应用程序。每个通知都会打开一个不同的 Activity (也有不同的附加功能)。 现在我想提取有
我有一个 Intent launchIntent = packageManagerForListener.getLaunchIntentForPackage(packagesForAdapter[po
List targetedShareIntents = new ArrayList(); Intent shareIntent = new Intent(android.content.Intent.
所以我试图在选择列表中的项目后启动一个新 Activity ......根据我所读的内容非常基本。我也在尝试在附加功能中发送一个值。所以我可以选择列表中的项目,然后新 Activity 开始,extr
有没有一种方法可以将一个Intent bundle 从一个 Intent 传递到另一个 Intent ,而不必提取包并单独处理每个额外的 Intent ? 例子: intent2.setExtras(
这个问题在这里已经有了答案: Android 5.0 (L) Service Intent must be explicit in Google analytics (11 个答案) 关闭 6 个月
我是一名优秀的程序员,十分优秀!