如何在运行时更改图像按钮图像?
ibtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
ibtn.setBackground
}
});
答案 0 :(得分:0)
首先从图库中选择一张图像,然后将该图像设置为按钮背景。
使用此功能从图库中选择图像
public void pickImageFromGallery() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
并在OnActivityResult
中接收您的图片 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == 1) {
final Bundle extras = data.getExtras();
if (extras != null) {
//Get image
Bitmap bitmap = extras.getParcelable("data");
ibtn.setImageBitmap(bitmap);
}
}
}