Android实现动态修改图片src

木来 木来

(1)有一个活动,并且在布局中实现一个button按钮和imageview控件,能正常的显示图片

(2)在活动中书写以下下面的代码,即可点击按钮,替换图片src

package com.shihsf.hello;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends BasicActivity implements View.OnClickListener{


    private static final String TAG = "MainActivity";

    private EditText editText;
private ImageView imageView;


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

        setContentView(R.layout.activity_main);


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


        Button button1=findViewById(R.id.main_button);
        editText=(EditText)findViewById(R.id.main_edittext);
        button1.setOnClickListener(this);


    }

    @Override    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_button:
                imageView.setImageResource(R.drawable.img2);




        }


    }
}
0 条评论