Android开发 - onclicklistener设置错误的图像

时间:2014-02-22 18:32:22

标签: android onclicklistener

package com.example.sliderapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

        // add functionality for android button
        MyClickListener listener = new MyClickListener();
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(listener);

        // add functionality for apple button
        MyClickListener2 listener2 = new MyClickListener2();
        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(listener2);

        // Add functionality for don't care button
        MyClickListener3 listener3 = new MyClickListener3();
        Button button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(listener3);

        //Add functionality for reset button
        MyClickListener4 listener4 = new MyClickListener4();
        Button button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(listener4);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * click listener method referring to android button
     * 
     * @author Ross
     * 
     */
    private class MyClickListener implements View.OnClickListener {

        public void onClick(View view) {

            ImageView imageView = (ImageView) findViewById(R.id.imageView1);

            imageView.setImageResource(R.drawable.happypng);
        }

    }

    /**
     * click listener method referring to apple button
     * 
     * @author Ross
     * 
     */
    private class MyClickListener2 implements View.OnClickListener {

        public void onClick(View view) {

            ImageView imageView = (ImageView) findViewById(R.id.imageView2);

            imageView.setImageResource(R.drawable.happypng);
        }

    }

    /**
     * Click listener referring to don't care button
     * @author Ross
     *
     */
    private class MyClickListener3 implements View.OnClickListener {

        public void onClick(View view) {

            ImageView imageView = (ImageView) findViewById(R.id.imageView1);

            imageView.setImageResource(R.drawable.whynotpng);

            ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);

            imageView2.setImageResource(R.drawable.whynotpng);
        }

    }

    /**
     * Click listener referring to Reset button
     * @author Ross
     *
     */
    private class MyClickListener4 implements View.OnClickListener {

        public void onClick(View view) {

            ImageView imageView = (ImageView) findViewById(R.id.imageView1);

            imageView.setImageResource(R.drawable.ic_launcher);

            ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);

            imageView2.setImageResource(R.drawable.apple_gray_logo);
        }

    }



}

在上一个方法(myClicklistener4)上面的代码中,我试图按照所述设置imageResources,但它将它们设置为等于.whynotpng,就像在第3类中一样。我不确定为什么会这样。

2 个答案:

答案 0 :(得分:1)

我无法告诉您错误发生在哪里,因为我在代码中看不到任何特别的错误。这是你缩短的代码。它现在有效吗?你是否已将所有可绘制的文件保存在正确的文件夹中并使用正确的名称?

package com.example.sliderapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView ivFirst, ivSecond;

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

        // register your button listeners
        ((Button) findViewById(R.id.button1)).setOnClickListener(this);
        ((Button) findViewById(R.id.button2)).setOnClickListener(this);
        ((Button) findViewById(R.id.button3)).setOnClickListener(this);
        ((Button) findViewById(R.id.button4)).setOnClickListener(this);
        // you can also define the onClick method via xml by calling   android:onClick="onClick"

        // save your ImageViews so that you don't have to find them for every buttonclick
        ivFirst = (ImageView) findViewById(R.id.imageView1);
        ivSecond = (ImageView) findViewById(R.id.imageView2);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View view) {
        // depending on which button is clicked do some action
        switch(view.getId()) {
            case R.id.button1:
                ivFirst.setImageResource(R.drawable.happypng);
                break;
            case R.id.button2:
                ivFirst.setImageResource(R.drawable.happypng);
                break;
            case R.id.button3:
                ivFirst.setImageResource(R.drawable.whynotpng);
                ivSecond.setImageResource(R.drawable.whynotpng);
                break;
            case R.id.button4:
                ivFirst.setImageResource(R.drawable.ic_launcher);
                ivSecond.setImageResource(R.drawable.apple_gray_logo);
                break;
        }
    }
}

答案 1 :(得分:0)

Endzeit ”已经解释了缩短代码的方法之一。

下面介绍了您可以遵循的另一种缩短代码的方法:

首先:

Add  android:onClick="method name" in all 4 Buttons in your xml file.

示例:

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="myClick"
        android:text="button1" />

同样为button2,button3和button 4

执行此操作

现在,在Java类中添加此代码

public class MainActivity extends Activity {

private ImageView imageView1, imageView2;

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

    imageView1 = (ImageView) findViewById(R.id.imageView1);
    imageView2 = (ImageView) findViewById(R.id.imageView2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void myClick(View view) {
    // depending on which button is clicked do some action
    switch(view.getId()) {
        case R.id.button1:
            imageView1.setImageResource(R.drawable.happypng);
            break;
        case R.id.button2:
            imageView2.setImageResource(R.drawable.happypng);
            break;
        case R.id.button3:
            imageView1.setImageResource(R.drawable.whynotpng);
            imageView2.setImageResource(R.drawable.whynotpng);
            break;
        case R.id.button4:
            imageView1.setImageResource(R.drawable.ic_launcher);
            imageView2.setImageResource(R.drawable.apple_gray_logo);
            break;
    }
  }
}

注意:在这种情况下,你不必找到按钮的ID。