0

一个activity中有图片按钮,我想把图片一个一个的加载到外部。这意味着除了第一个之外,所有的图片按钮一开始都是不可见的。单击按钮后,我应该能够加载图像,并且在按钮上加载图像后,应该会显示下一个按钮。我为第一个按钮实现了这一点,但无法为其余按钮做到这一点。这是我的代码。

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);


    }

}
4

2 回答 2

0

您不知何故需要知道最后一次单击的按钮,以便您可以设置图像和下一个的可见性。以下实现了这一点。

public class MainActivity extends Activity implements View.OnClickListener {

    private static int RESULT_LOAD_IMAGE = 1;

    ImageButton[] imgButtons;
    private ImageButton lastClicked;

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

        imgButtons = new ImageButton[6];
        imgButtons[0] = (ImageButton) findViewById(R.id.imageButton1);
        imgButtons[1] = (ImageButton) findViewById(R.id.imageButton2);
        imgButtons[2] = (ImageButton) findViewById(R.id.imageButton3);
        imgButtons[3] = (ImageButton) findViewById(R.id.imageButton4);
        imgButtons[4] = (ImageButton) findViewById(R.id.imageButton5);
        imgButtons[5] = (ImageButton) findViewById(R.id.imageButton6);

        for(ImageButton imgButton : imgButtons){
            imgButton.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(
            Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        lastClicked = (ImageButton) v; // note, only possible because we only have imageButtons with click listener
        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();

            if(lastClicked != null){
                lastClicked.setImageBitmap(BitmapFactory.decodeFile(picturePath));
                int numButtons = imgButtons.length;
                for(int i=0; i<numButtons; i++){
                    if(imgButtons[i] == lastClicked && ++i < imgButtons.length){
                        imgButtons[i].setVisibility(View.VISIBLE);
                        break;
                    }
                }
            }
        }

    }
}
于 2013-10-28T09:49:07.107 回答
0

创建选择图像的方法。创建一个字符串全局变量,例如“ which”。

喜欢...

public void getImage(String whichbutton){
   Intent intent = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(intent, RESULT_LOAD_IMAGE);
            which=whichbutton;
}

现在在每次单击按钮时调用此方法并传递按钮名称。

在您的 onActivityResult 中,根据哪个变量的值更改图像。

@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();


if(which.equals("a")){
                ImageButton a = (ImageButton)findViewById(R.id.imageButton1);
                a.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                b.setVisibility(b.VISIBLE);
}else if(which.equals("b")){

 ImageButton b = (ImageButton)findViewById(R.id.imageButton2);
                b.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                c.setVisibility(b.VISIBLE);

}//and more


        }
于 2013-10-28T09:49:49.557 回答