按钮工作,ImageButtons不工作

时间:2013-07-16 13:05:19

标签: java android button imagebutton

我在ImageButton for Android上搜索过很多参考资料。并且完全相同,但它似乎没有用。奇怪的是,它只在我使用'Button'时起作用。 我错过了一些重要的部分吗?

有人可以帮我解决我的问题吗? 我在下面列出了我的代码。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Button connectBtn = (Button)this.findViewById(R.id.connectBtn); //<<<=== THIS WORKS
    ImageButton connectBtn = (ImageButton) this.findViewById(R.id.connectBtn); // <<<=== THIS NOT
    ImageButton getPy = (ImageButton) findViewById(R.id.getFilePy); // <<<=== THIS NOT
    ImageButton runPy = (ImageButton) findViewById(R.id.runPy); // <<<=== THIS NOT

    connectBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mySetConfig();
        }
    });

    getPy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showFileChooser();
        }
    });
    runPy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pythonButton();
        }
    });
}

XML:          

<ImageButton
    android:id="@+id/connectBtn"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:background="@drawable/button_1" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="5dp" />

<ImageButton
    android:id="@+id/getPy"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:background="@drawable/button_2" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ImageButton
    android:id="@+id/runPy"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:background="@drawable/button_3" />

 <FrameLayout
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</FrameLayout>

<EditText
    android:id="@+id/app_status"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:ems="10"
    android:singleLine="false" >

    <requestFocus />
</EditText>

</LinearLayout>

提前感谢你的好心。

1 个答案:

答案 0 :(得分:0)

将click Listener放入ImageButton的正确方法:

    ImageButton connectBtn = (ImageButton) findViewById(R.id.connectBtn);

    connectBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
                      mySetConfig();

        }

    });

这对你来说是一个不错的tut。 请从herE中删除“this”这个词:

ImageButton connectBtn =(ImageButton) this .findViewById(R.id.connectBtn); &lt;&lt;&lt; ===这不是