andriod studio点击按钮显示图片

(1)把要显示的图片直接拷贝到目录

app\src\main\res\drawable

(2)使用imageview控件和两个按钮

(3)简单代码,记得定义未初始化前=null,否则害死人

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    private ImageView imageView = null;
    private Button bt1 = null;//图1
    private Button bt2 = null;//图2

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

        imageView = (ImageView) findViewById(R.id.imageView);
        bt1 = (Button) findViewById(R.id.button);
        bt2 = (Button) findViewById(R.id.button2);
        bt1.setOnClickListener(listener);
        bt2.setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        public void onClick(View v) {
            if (v == bt1) {
                //   currentImgId=(currentImgId-1+imgId.length)%imgId.length;
                //    imageView.setImageResource(imgId[currentImgId]);
                imageView.setImageResource( R.drawable.tu1);

            }
            if (v == bt2) {
                //  currentImgId=(currentImgId+1)%imgId.length;
                //  imageView.setImageResource(imgId[currentImgId]);
                imageView.setImageResource( R.drawable.tu2);
            }

        }
    };
}

你可能感兴趣的:(安卓studio)