android imageButton onClick方法没有调用

tal*_*ore 3 android onclick imagebutton

编辑:发布完整的代码(除了XML,因为它是一堆荒谬的表格格式!)请忽略与我的问题无关的代码!我现在正在提供功能.我稍后会清理它.

大家好!第一个应用和第一个问题.我在这里研究了一段时间,通常会找到我的答案,但我有一个可能非常明显的bugger.我有一个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>
Run Code Online (Sandbox Code Playgroud)

我的代码:

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);

}
Run Code Online (Sandbox Code Playgroud)

}

好像很简单吧?当我点击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
Run Code Online (Sandbox Code Playgroud)

之后,当我点击imageButton时什么也没做

Anj*_*nju 10

你的代码错了.你没有使用ClickListener分配你的按钮.

请参阅此示例代码以了解如何实现它.

public class ExampleActivity extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
        Button button = (Button)findViewById(R.id.corky);
        button.setOnClickListener(this);
    }

    // Implement the OnClickListener callback
    public void onClick(View v) {
      // do something when the button is clicked
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

您可以访问此链接以获取更多参考.

  • 不需要 - 在较新的Android版本中,您可以在没有"OnClickListener"的情况下使用onClick. (4认同)