活動中有圖像按鈕,我想逐個加載圖像。它意味着除第一個圖像按鈕外,所有圖像按鈕都是不可見的。當我點擊按鈕後,我應該可以加載圖像,並且在圖像加載到按鈕上後,下一個按鈕應該會顯示。我爲第一個按鈕實現了這一點,但無法爲其餘部分做到這一點。這是我的代碼。逐個動態添加圖像
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
ImageButton a, b, c, d, e, f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = (ImageButton) findViewById(R.id.imageButton1);
b = (ImageButton) findViewById(R.id.imageButton2);
c = (ImageButton) findViewById(R.id.imageButton3);
d = (ImageButton) findViewById(R.id.imageButton4);
e = (ImageButton) findViewById(R.id.imageButton5);
f = (ImageButton) findViewById(R.id.imageButton6);
a.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageButton a = (ImageButton)findViewById(R.id.imageButton1);
a.setImageBitmap(BitmapFactory.decodeFile(picturePath));
b.setVisibility(b.VISIBLE);
}
}