android imageButton onClick 方法未调用
编辑:发布完整代码(XML除外,因为它是一堆荒谬的表格格式!)请忽略与我的问题无关的代码!我现在刚刚获得功能。稍后我会清理它。
第一个应用程序和第一个问题。我在这里研究了一段时间,通常都能找到答案,但我有一个可能非常明显的问题。我有一个 imageButton 似乎没有调用分配的方法。
我的 imageButton 的 XML:
<ImageButton
android:background="@null"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton1"
android:src="@drawable/stats"
android:layout_gravity="center_vertical">
</ImageButton>
我的代码:
package com.talismancs;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class sheet extends Activity{
private String selection;
private String pick;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sheet);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
// Get extra from .main and remove spaces
String pick = extras.getString(selection);
pick = pick.replace(" ", "");
//Convert extra from string to int as ID and make image
int imageResource = getResources().getIdentifier(pick, "drawable", getPackageName());
ImageView iv = (ImageView) findViewById(R.id.imageView1);
final Drawable image = getResources().getDrawable(imageResource);
iv.setImageDrawable(image);
// Populate tv's from string
TextView tv1 = (TextView) findViewById(R.id.textView4);
TextView tv2 = (TextView) findViewById(R.id.textView5);
TextView tv3 = (TextView) findViewById(R.id.textView6);
TextView tv4 = (TextView) findViewById(R.id.textView7);
TextView tv5 = (TextView) findViewById(R.id.textView8);
int arrayresource = getResources().getIdentifier(pick, "array", getPackageName());
String[] CharString = getResources().getStringArray(arrayresource);
tv1.setText(CharString[0]);
tv2.setText(CharString[1]);
tv3.setText(CharString[2]);
tv4.setText(CharString[3]);
tv5.setText(CharString[4]);
}
}
public void onClick(View view) {
Intent i = new Intent(sheet.this, stats.class);
i.putExtra(pick, pick);
startActivity(i);
}
}
看起来很简单,对吗?当我单击 imageButton 时,它完全没有任何反应!
请帮忙。
编辑:LOGCAT 选择一个微调项目后,我们可以进入此活动 .sheet
> 03-16 06:15:38.977:
> INFO/ActivityManager(563): Displayed
> activity com.talismancs/.sheet: 766 ms
> 03-16 06:15:42.907:
> DEBUG/dalvikvm(1735): GC freed 448
> objects / 39160 bytes in 58ms 03-16
> 06:15:43.847:
> INFO/NotificationService(563):
> enqueueToast pkg=com.talismancs
> callback=android.app.ITransientNotification$Stub$Proxy@43773720
> duration=1 03-16 06:15:43.877:
> INFO/ActivityManager(563): Starting
> activity: Intent {
> comp={com.talismancs/com.talismancs.sheet} (has extras) } 03-16 06:15:43.917:
> WARN/InputManagerService(563): Window
> already focused, ignoring focus gain
> of:
> com.android.internal.view.IInputMethodClient$Stub$Proxy@43718320
> 03-16 06:15:44.527:
> INFO/ActivityManager(563): Displayed
> activity com.talismancs/.sheet: 646 ms
之后,当我单击 imageButton 时,它什么也不做
EDIT: Posting full code (except XML as it a bunch of ridiculous table formatting!) Please ignore the code that doesn't pertain to my question! I am just getting functionality right now. I'll clean it up later.
First app and first question. I've researched here a while and usually find my answer but I have a bugger that is probably very obvious. I have an imageButton that doesn't seem to be calling the method assigned.
My XML for my imageButton:
<ImageButton
android:background="@null"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton1"
android:src="@drawable/stats"
android:layout_gravity="center_vertical">
</ImageButton>
My code:
package com.talismancs;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class sheet extends Activity{
private String selection;
private String pick;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sheet);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
// Get extra from .main and remove spaces
String pick = extras.getString(selection);
pick = pick.replace(" ", "");
//Convert extra from string to int as ID and make image
int imageResource = getResources().getIdentifier(pick, "drawable", getPackageName());
ImageView iv = (ImageView) findViewById(R.id.imageView1);
final Drawable image = getResources().getDrawable(imageResource);
iv.setImageDrawable(image);
// Populate tv's from string
TextView tv1 = (TextView) findViewById(R.id.textView4);
TextView tv2 = (TextView) findViewById(R.id.textView5);
TextView tv3 = (TextView) findViewById(R.id.textView6);
TextView tv4 = (TextView) findViewById(R.id.textView7);
TextView tv5 = (TextView) findViewById(R.id.textView8);
int arrayresource = getResources().getIdentifier(pick, "array", getPackageName());
String[] CharString = getResources().getStringArray(arrayresource);
tv1.setText(CharString[0]);
tv2.setText(CharString[1]);
tv3.setText(CharString[2]);
tv4.setText(CharString[3]);
tv5.setText(CharString[4]);
}
}
public void onClick(View view) {
Intent i = new Intent(sheet.this, stats.class);
i.putExtra(pick, pick);
startActivity(i);
}
}
Seems simple right? When I click the imageButton it does absolutely nothing!
Please help.
EDIT: LOGCAT After selecting a spinner item which gets us to this activity .sheet
> 03-16 06:15:38.977:
> INFO/ActivityManager(563): Displayed
> activity com.talismancs/.sheet: 766 ms
> 03-16 06:15:42.907:
> DEBUG/dalvikvm(1735): GC freed 448
> objects / 39160 bytes in 58ms 03-16
> 06:15:43.847:
> INFO/NotificationService(563):
> enqueueToast pkg=com.talismancs
> callback=android.app.ITransientNotification$Stub$Proxy@43773720
> duration=1 03-16 06:15:43.877:
> INFO/ActivityManager(563): Starting
> activity: Intent {
> comp={com.talismancs/com.talismancs.sheet} (has extras) } 03-16 06:15:43.917:
> WARN/InputManagerService(563): Window
> already focused, ignoring focus gain
> of:
> com.android.internal.view.IInputMethodClient$Stub$Proxy@43718320
> 03-16 06:15:44.527:
> INFO/ActivityManager(563): Displayed
> activity com.talismancs/.sheet: 646 ms
After that is does nothing when I click the imageButton
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的代码是错误的。您还没有为您的按钮分配 ClickListener。
请参阅此示例代码以了解如何实现它。
您可以访问此链接以获取更多参考。
Your code is wrong. You havent assigned ur button with ClickListener.
Please see this sample code for how to implement it.
You may visit this link for more reference.
Vikias 是对的,只是不需要重写 onCreate。原始代码唯一的问题在于 IMageButton 的 XML 布局文件。
ImageButton 声明了
android:onClick="click"
但活动中的代码不包含方法click
,而是包含方法onClick
> ImageButton 的 XML 中未使用它。这就是为什么什么也没有发生。Vikias is right except that one doesn't need to override onCreate. The only thing wrong with the orginal code lies in the XML-layout file for the IMageButton.
The ImageButton declares
android:onClick="click"
but the code in the activity doesn't contain a methodclick
, instead is has a methodonClick
which isn't used in the XML for the ImageButton. That's why nothing happens.我确信首先您必须重写
onCreate
方法。同时加载您想要在Activity
中使用的布局资源。所以你的代码应该是这样的:
还尝试以标准方式使用类名。就像每个类名都应该以大写字母开头一样。
检查此以获取更多参考。
I am sure first you must override the
onCreate
method. Also load that layout resources you want to use in yourActivity
.So your code should be like this:
Also try to use class name in standard way. Like every class name should start with a capital letter.
Check this for further references.
与OP的问题无关,但是:
如果您发现按钮在某些设备上运行正常的程序中不起作用(您有兆字节的源代码,但您不是作者),
这可能是因为程序故意阻止了这些按钮。
至少可以通过两种方式完成:
1)代码通过 setEnabled() 禁用按钮,
2)按钮上方可能有一个透明层,
3)透明层甚至可以成为一项透明的活动。
(其余的都是微不足道的:某些条件在程序运行的设备上是正确的,但在新设备上总是错误的......如果市场应用程序表现得像这样,那会很奇怪,但如果你在公司工作,那就不那么不合理了制造设备。)
Not related to the OP's problem, but:
If you see that buttons do not work in a program that works ok on some device (you have megabytes of source but you are not the author),
this may be because the buttons are intentionally blocked by the program.
It may be done in at least two ways:
1) the buttons are disabled by the code via setEnabled(),
2) there may be a transparent layer above the buttons,
3) the transparent layer may even be a transparent activity.
(The rest is trivial: some condition was true on the device where the program worked but is always false on the new device... It would be weird if a market application behaved like that, but not so unreasonable if you work in a company that manufactures devices.)
一切看起来都不错,只需尝试进行以下更改..
然后在 onCreate 中使用
它应该可以工作。否则调试你的代码并查看你的控件去哪里了。
Everything seems ok just try to do following changes..
then in onCreate use
It should work. Else debug your code and see where your control is going.