如何在imageview中添加点击功能?

时间:2013-03-08 13:33:10

标签: android

如何在图像视图中设置onClick功能?

当用户点击图片时,它应该以全屏显示在下一页上。

以下是应添加onClick功能的现有代码。

btnLO = new LinearLayout(MainActivity1.this);   
LinearLayout.LayoutParams paramsLO = new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// button margins
paramsLO.setMargins(0, 0, 0, 0);

LinearLayout.LayoutParams paramsLO2 = new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// button margins
paramsLO2.setMargins(0, 20, 0, 0);
// button height/width *pixels*
btnLO.setOrientation(LinearLayout.VERTICAL);
btnLO.setBackgroundColor(5); // not working correctly

//buttons
for (i =0;i <reqdata.length;i++) {
    LinearLayout li=new LinearLayout(this);
    li.setOrientation(LinearLayout.HORIZONTAL);
    final Button b1 = new Button(MainActivity1.this);
    final ImageView imageView = new ImageView(MainActivity1.this);

    int width = 160;
    int height = 50;

    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(
        width,height);
    parms.setMargins(0, 2, 0, 0);
    imageView.setLayoutParams(parms);

    li.addView(b1, paramsLO);        
    li.addView(imageView);
    btnLO.addView(li);

    b1.setText(reqdata[i].getSpinnerText());

    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {               
            imageView1 = imageView;
            Intent pictureActionIntent = new  
            Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

            pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new  
            File(SDCARD_ROOT_PATH + SAVE_PATH_IN_SDCARD,IMAGE_CAPTURE_NAME)));  

            startActivityForResult(pictureActionIntent,CAMERA_PICTURE);
            b1.setClickable(false);
        }   
    });    

}
final Button b2 = new Button(MainActivity1.this);

b2.setText("Submit");
b2.setWidth(150);
b2.setHeight(50);
b2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {                            
        long visitID = dbConnector.saveVisit();
        for (i =0;i <reqdata.length;i++) {
            dbConnector.saveVisitDetail(listByte.get(i),visitID,Long.
            valueOf(reqdata[i].getValue()).longValue());
        }      
        Toast.makeText(getBaseContext(),  
        "Sucessful",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity1.this, Main.class);     
        startActivity(intent);
        finish();
    }
}); 
btnLO.addView(b2, paramsLO2);
btnLO.setGravity(Gravity.CENTER| Gravity.CENTER_HORIZONTAL);
scroll.addView(btnLO);

this.addContentView(scroll, new LayoutParams());

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    if (imageReturnedIntent.getExtras() != null) {
        // here is the image from camera
        yourSelectedImage = (Bitmap)  
        imageReturnedIntent.getExtras().get("data");
        ByteArrayOutputStream outStr = new  
        ByteArrayOutputStream();                  

        yourSelectedImage.compress(CompressFormat.JPEG, 100, outStr);
        blob = outStr.toByteArray();
        yourSelectedImage =  BitmapFactory.decodeByteArray(blob, 0,
                                                           blob.length);
        imageView1.setImageBitmap(yourSelectedImage);
        listByte.add(blob);
    }
}

1 个答案:

答案 0 :(得分:1)

这是您可以添加点击图片视图

的方法
imageview.setOnClickListener(new View.OnClickListener() 
     {
         public void onClick(View v) 
         {

         }

});