- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个使用 onClickListeners 来翻转图 block 的应用程序。我在 RelativeLayout 中拥有全部内容。然后我有第二个 RelativeLayout 来包含我所有的图 block ,以便于清除和添加。我还在 xml 中预定义了各种其他按钮。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/boardlayout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/board"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<ImageView
android:id="@+id/menuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/skill2Button"
android:layout_alignParentRight="true"
android:src="@drawable/ic_menu" />
</RelativeLayout>
在代码中,我添加了 Tiles,它们是具有我的自定义操作的 ImageView。我还向它们添加了 onClickListeners。 grid 变量是我上面显示的 mainlayout RelativeLayout。
private void createTiles(){
//Create the tiles
int j = 0;
Tile t;
for(int i = 0; i < 30; i++){
int r = i/5;
int c = j%5;
t = new Tile(this, r, c);
t.setOnClickListener(tileClicked);
j++;
if(j == 5)
j=0;
grid.addView(t);
}
}
问题在于较旧的 Android 版本,这些图 block 似乎永远不会触发点击事件。我将 onClickListeners 添加到图 block ,然后在 RelativeLayout 上添加一个只是为了测试,RelativeLayout 获取事件,但图 block 没有。
菜单 ImageView(在上面的 xml 中显示)也有一个 onClickListener,它会被触发。
在较新的 Android 版本中,这可以正常工作。这让我想知道旧版本是否存在从代码中添加的 View 传递事件的问题。
有人可以告诉我旧版本做错了什么吗?
最佳答案
试试这个,
private void createTiles(){
//Create the tiles
int j = 0;
Tile t[30];
for(int i = 0; i < 30; i++){
int r = i/5;
int c = j%5;
t = new Tile(this, r, c);
t[i].setOnClickListener(this);
j++;
if(j == 5)
j=0;
grid.addView(t);
}
}
您需要在您的类中实现 OnClickListener。然后你必须重写 onClick 方法:
@Override
public void onClick(View v) {
switch(v.getId()){
case // switch the ids of your views by calling getId method on them
}
}
关于Android onClickListener 不触发 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16174185/
当我单击图像时,它不会更改显示 TextView ,我哪里出错了? **List fields = new ArrayList(); for(int i = 0; i < 64; i+
我想做的是提供一个平台列表供用户选择。通过单击一个平台,它会将相应的 sql 表名称的名称放入变量中。但我不知道如何在我的提交按钮 clickListener 中使用该变量。这是我正在使用的代码部分。
我可以在我的 recyclerView holder 中实现它们 public class PagosPendientesViewHolder extends RecyclerView.ViewHol
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
当我想向 setOnClickListener 添加参数(例如按钮)时,我从 eclipse 警报中单击 View.OnClickListener() ,但 eclipse 类型 new OnClic
我看了http://developer.android.com/reference/android/view/package-summary.html并看到 View 类有一个名为“View.OnCl
我正在尝试 android 上的示例应用程序。我可以获得输出。 studentFormsActiviyt.java package com.example.android.accelerometerp
我有一个关于实现 OnClickListeners 以使用 ADT 进行开发的问题。我不确定哪种方法更有效,谁能告诉我每种方法的优缺点? class x extends Activity implem
我要问一个非常基本的问题,但我被困了很长时间。 在卡片 View 之后有一个回收 View ,每行有 2 个图像。现在我想在图像上创建点击监听器而不是 recycleview。 这个 Activity
我的理解是,当我创建一个监听点击的按钮对象时,我必须: 创建按钮对象 使用OnClickListner让它监听用户的点击 使用onClick在用户点击按钮后执行 Action 现在, setOnCli
我正在学习 android 开发的初学者教程,但我不明白这个错误。 错误在行“callButton.setOnClickListener(new OnClickListener() {” 是的,我知道
当使用下面的代码时,我不断收到错误,我知道还有另一种使用 onclick 函数的方法,android:onclick ...但我更喜欢这种“更清洁”的方式。 textview 的 id 名称是正确的,
尝试将 onClickListener 添加到我的 listView 中的项目时,我收到一条错误消息:“View 类型中的方法 setOnClickListener(View.OnClickListe
我有一个问题...我有不同的 TextView 和一个监听器。像这些: help_modul.setOnClickListener(this); help_timetable.setO
我创建了一个自定义的 AutocompleteAdapter,我需要允许用户选择某个对象(客户)。 我的 AutoCompleteAdapter 看起来像这样: package nl.raakict.
我有一个程序,它有一个用户可以编辑的文本框。当用户按下按钮时,会创建一个对话框,显示用户的文本和确认“是/否”选项。 public void setIP(View v){ //get the
这个问题在这里已经有了答案: NullPointerException accessing views in onCreate() (13 个回答) What is a NullPointerExce
我有一个自定义 View ,我想当我设置 onClickListener 时我总是会崩溃。 public class Holder extends View implements OnClickLis
我有一组复选框,它们都需要 OnClickListener 来查看它们何时被选中/取消选中。这是我所拥有的: CheckBox[] sens = new CheckBox[3]; for (int i
我有两个简单的不同类,其中一个类扩展了另一个类。我的第一个类是: public class MyObject {} 里面没有任何东西。 还有另一个: public class People exten
我是一名优秀的程序员,十分优秀!