2013-10-28 55 views
0

活动中有图像按钮,我想逐个加载图像。它意味着除第一个图像按钮外,所有图像按钮都是不可见的。当我点击按钮后,我应该可以加载图像,并且在图像加载到按钮上后,下一个按钮应该会显示。我为第一个按钮实现了这一点,但无法为其余部分做到这一点。这是我的代码。逐个动态添加图像

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


    } 

} 

回答

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

    } 
} 
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 


     }