0

我正在尝试创建一个测试应用程序,当我按下 ImageButton 时,会出现一条日志消息。简单的。

我希望 ImageButton 视图与一个类一起工作。

我这样做了: 更新为正确的构造函数名称

public class MainActivity extends Activity {


  MyButtonClass btnOk = null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        btnOk = new ButtonClass(this);      
        setContentView(R.layout.activity_main);
  }
}


class MyButtonClass extends ImageButton{

        public MyButtonClass(Context context) {
            super(context);

            findViewById(R.id.btButton);

              OnClickListener oclBtnOk = new OnClickListener() {
                  @Override
                  public void onClick(View v) {
                    // change text of the TextView (tvOut)
                   Log.e("Log This:","Yay! I am working!");
                  }
                };

                setOnClickListener(oclBtnOk);
        }



    }

我的布局 xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/btButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

当我运行应用程序时,我在 Log cat 上没有收到任何错误,并且应用程序没有退出但是当我按下 ImageButton 时它没有做任何事情:/

4

3 回答 3

2

首先,您的 Java 代码将无法编译,因为MyButtonClass它不是有效的 Java 类。

其次,即使您将构造函数修复为MyButtonClass,也会在运行时崩溃,因为您正试图找到MyButtonClassvia的子代findViewById(),并且没有子代。

第三,即使它确实编译了,即使你在构造函数中正确实现了逻辑,你也没有MyButtonClass在你的布局资源中引用,所以它不会被使用。

第四,如果您花时间阅读 Google 和其他有经验的 Android 开发人员编写的代码,您会发现很少有人(如果有的话)会以这种方式使用继承。您根本不需要MyButtonClass,因为您的活动或片段可以(并且应该)设置侦听器。换句话说,喜欢组合而不是继承

于 2013-08-02T12:52:52.797 回答
1

怎么搞得这么乱。您的代码需要大量修剪。你可以试试这段代码

    public class MyAndroidAppActivity extends Activity {

ImageButton imageButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

}

public void addListenerOnButton() {

    imageButton = (ImageButton) findViewById(R.id.imageButton1);

    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

           Toast.makeText(MyAndroidAppActivity.this,
            "ImageButton is clicked!", Toast.LENGTH_SHORT).show();

        }

    });

}

    }

祝你好运=)

于 2013-08-02T12:54:20.820 回答
0

采用

@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageButton ib = (ImageButton)findViewById(R.id.btButton);      
        ib.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.e("Log This:","Yay! I am working!");

        }
    });
  }
}
于 2013-08-02T12:51:49.897 回答