- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使我随机生成的图像(被解释为按钮)变得可点击时遇到了问题。每个都会导致不同的 Activity 。
随机图像实际上工作完美,唯一的问题是不可点击。
这是我的 Main.java:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final List<String> images = new ArrayList<String>();
for (int i=1; i<=13; i++) {
images.add("img"+i);
}
final Button imgView = (Button)findViewById(R.id.top1);
String imgName = null;
int id = 0;
Collections.shuffle(images, new Random());
imgName = images.remove(0);
imageRandomizer(imgName, id, imgView);
}
public void imageRandomizer(String imgName, int id, final Button imgView) {
id = getResources().getIdentifier(imgName,
"drawable",
getPackageName());
imgView.setBackgroundResource(id);
}
}
在我的布局中,我将 id top1
指定为 Button
。所以上面的代码将查找我的可绘制图像,它们的名称为 img1.jpg、img2.jpg、img3.jpg,直到img13.jpg。
使 ImageButton
可点击一个 Activity 而不依赖于显示的随机图像很容易,我可以毫无问题地做到这一点。但我想做的是,当 img1.jpg 生成时,它变得可点击并导致 Activity1.java,对于 img2.jpg Intent 转到 Activity2.java 等
编辑@Roflcoptr这是我的 OnClickListener:
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
switch((Integer) v.getTag()) {
case 1:
Intent aid = new Intent(Main.this, ProjektAID.class);
startActivity(aid);
case 2:
Intent adh = new Intent(Main.this, ProjektADH.class);
startActivity(adh);
case 3:
Intent bos = new Intent(Main.this, ProjektBOS.class);
startActivity(bos);
case 4:
Intent brot = new Intent(Main.this, ProjektBROT.class);
startActivity(brot);
case 5:
Intent care = new Intent(Main.this, ProjektCARE.class);
startActivity(care);
case 6:
Intent caritas = new Intent(Main.this, ProjektCARITAS.class);
startActivity(caritas);
case 7:
Intent doc = new Intent(Main.this, ProjektDOC.class);
startActivity(doc);
case 8:
Intent drk = new Intent(Main.this, ProjektDRK.class);
startActivity(drk);
case 9:
Intent give = new Intent(Main.this, ProjektGIVE.class);
startActivity(give);
case 10:
Intent hive = new Intent(Main.this, ProjektHIV.class);
startActivity(hive);
case 11:
Intent jo = new Intent(Main.this, ProjektJOHANNITER.class);
startActivity(jo);
case 12:
Intent kind = new Intent(Main.this, ProjektKINDERHERZ.class);
startActivity(kind);
case 13:
Intent kult = new Intent(Main.this, ProjektKULTURGUT.class);
startActivity(kult);
}
}
};
这是随机发生器方法:
public void imageRandomizer(String imgName, int id, final Button imgView) {
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
imgView.setTag(new Integer(1)); //example for image 1
if (imgName.equals("img1")) {
imgView.setTag(new Integer(1)); //example for image 1
} else if (imgName.equals("img2")) {
imgView.setTag(new Integer(2));
} else if (imgName.equals("img3")) {
imgView.setTag(new Integer(3));
} else if (imgName.equals("img4")) {
imgView.setTag(new Integer(4));
} else if (imgName.equals("img5")) {
imgView.setTag(new Integer(5));
} else if (imgName.equals("img6")) {
imgView.setTag(new Integer(6));
} else if (imgName.equals("img7")) {
imgView.setTag(new Integer(7));
} else if (imgName.equals("img8")) {
imgView.setTag(new Integer(8));
} else if (imgName.equals("img9")) {
imgView.setTag(new Integer(9));
} else if (imgName.equals("img10")) {
imgView.setTag(new Integer(10));
} else if (imgName.equals("img11")) {
imgView.setTag(new Integer(11));
} else if (imgName.equals("img12")) {
imgView.setTag(new Integer(12));
}
else if (imgName.equals("img13")) {
imgView.setTag(new Integer(13));
}
}
最佳答案
我会使用标签来识别按钮。因此,在您的 imateRandomizer 中为每个可能的图像添加一个唯一的 ID。我不知道如何唯一地识别图像,但我将在此处显示名称的示例:
public void imageRandomizer(String imgName, int id, final Button imgView)
{
id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
if (imgName.equals("Name of the Image for your first activity") {
imgView.setTag(new Integer(1)); //example for image 1
} else if (imgName.equals("Name of the Image for your second activity") {
imgView.setTag(new Integer(2));
}
}
然后在您的 ClickListener 中,您可以检查按钮具有哪个标签:
imgView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch((Integer) v.getTag()) {
case 1: //start Activity 1;
break;
case 2: //start Activity 2;
break;
}
}
});
关于android - 使随机生成的 ImageButtons 可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5535918/
我使用类 java 来自定义圆形图像按钮。现在我想将它转换为 ImageButton。但是给我错误ClassCastException。我需要帮助 !如果你有决心请给我。谢谢。 我用 rootView
我设置了一些样式来处理不同的 API(一种用于 v21,另一种用于任何低于该版本的 API)。我想为我的 ImageButton 设计一种样式,但它似乎没有按我预期的方式工作。 v-21 的风格是
所以我设置了 4 个普通图像按钮,然后是第 5 个图像按钮,它是一个拖放按钮。我希望 4 个非拖放图像按钮保留在特定位置,无论拖放图像按钮发生什么情况。当我移动拖放图像按钮时,其他图像按钮也会移动并且
我在后面的代码中创建了一个 ImageButton。现在我需要当客户端按下 ImageButton 时,ImageButton 将执行特定功能。问题是它不执行此特定功能。 这是代码- var img
我知道如何在单击时更改 ImageButton 的 src,但是当时我还想更改另一个 ImageButton 的 src。我不知道如何访问未单击的 ImageButton。我知道是身份证 编辑:值得一
其实我有两个问题: (1) 是否可以将另一个图像放在已经设置了 ImageUrl 的 ImageButton 之上(无需更改 ImageUrl - 字面上只是将第二个图像添加到“顶部”)?甚至通过使用
这是一张可以帮助你理解我的问题的图片: 我想将 ImageButton 中显示的图像拉伸(stretch)到 ImageButton 的整个区域。可以看到,编号为1的图片只占ImageButton的9
找不到以下类: - ImageButton(更改为android.widget.ImageButton、修复构建路径、编辑 XML) 当我使用 android:src="@drawable/chang
我正在开发一个应用程序,在某些时候它会显示一些“学习表”。问题是,对于每个项目,它应该显示两个按钮,这些按钮应该重叠在卡片 View 上,但它只适用于普通按钮,而使用图像按钮则不会发生这种情况。
如果我使用带有选择器的 ImageButton 作为其背景,是否有我可以更改的状态来改变它的外观?现在我可以让它在按下时更改图像,但似乎没有“突出显示”或“选择”或类似状态让我随意切换它的外观。 这是
我刚刚开始使用我的第一个音板。基本上这就是我到目前为止所拥有的(除了我有 40 个声音)。有谁知道更好的方法来做到这一点?我必须去约会,但我今天晚些时候会回来回复。谢谢你,任何可以帮助的人。 ----
我的“删除按钮(图像按钮)”的单击事件有问题。我知道大家对此还有其他疑问,但我什么也不明白! 我没听懂没有答案!我做了所有其他问题中指定的所有内容!此页面中的其他事件使“重定向”到示例,效果很好! 我
这是我的问题的后续: Asp:Label is not shown when visible is set to true? 在上面的问题中,我有一些控件的可见性未设置为 false,因为它们不在 U
我正在尝试创建一个测试应用程序,当我按下 ImageButton 时,会出现一条日志消息。简单。 我希望 ImageButton View 能够与类一起使用。 我是这样做的:更新了正确的 builde
嘿,我正在尝试进行对话。但整个对话框没有显示,只有底部 3 个图像按钮显示:这里出了什么问题?当我单击对话框内的按钮时,即使我做了一个开关盒,也没有任何反应。 customtype_dialog.xm
我正在尝试动态声明一个 ImageButton。 我声明它并为其分配一个 ID 和图像,如下所示: ImageButton btn = new ImageButton(); btn.ImageUrl
在我的应用程序中,我可以使用此类缩放 ImageView `公共(public)类 resizeAvatar 扩展 View { private final Drawable sfondoAvatar
我已将我的按钮设置为按 Facebook、Instagram、Twitter 和 Reddit 的顺序从上到下排列,但它没有这样做,它只显示 Facebook 和 Reddit,我不知道为什么。我有我
我是 Android 新手 我需要动态移动 ImageButton,就像当我单击按钮时它需要重新定位到一个新位置。我正在使用 AbsoluteLayout。 最佳答案 永远不要使用 AbsoluteL
我无法让 ImageButton 显示在屏幕右侧。如果我将 ImageButton 放在内部 LinearLayout 上方,它将显示在左侧,但是当我将它放在内部 LinearLayout 下方时,它
我是一名优秀的程序员,十分优秀!