我有幾個ImageViews,都使用onClick()。如何根據所點擊的圖像從特定的ImageView中獲取ID或R.drawable int以打開另一個圖像?在onClick()中獲取R.drawable int
0
A
回答
1
我想你有你的XML佈局文件中的一些圖像的觀點:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_content" >
<ImageView
android:id="@+id/image_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_drawable_1"
android:onClick="doSomething" />
<ImageView
android:id="@+id/image_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_drawable_2"
android:onClick="doSomething" />
<ImageView
android:id="@+id/image_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_drawable_3"
android:onClick="doSomething" />
</LinearLayout>
你要這個方法添加到您的活動:
public void doSomething(View v) {
switch (v.getId()) {
case R.id.image_1:
// You clicked on image 1
// If you need to do something to the ImageView with this particular Id then uncomment the next line.
// ImageView imageView = (ImageView) v;
break;
case R.id.image_2:
// You clicked on image 2
break;
case R.id.image_3:
// You clicked on image 3
break;
}
}
-1
您可以使用以下命令從ImageView中獲取當前Drawable。
Drawable myDrawable = YourImageView.getDrawable();
然後,您可以繼續並使用myDrawable
任何你想要的地方。
0
我不認爲你可以獲取用於爲ImageView生成Drawable的資源標識符,但是當以編程方式設置圖像時,可以將其另存爲ImageView的標記。
imageView.setImageResource(R.drawable.image_resource);
imageView.setTag(R.drawable.image_resource);
可以不是以後這樣的檢索:
int imageResource = (Integer) imageView.getTag();
1
使用getResources().getDrawable
例子:
ImageView myImg = (ImageView) findViewById(R.id.myImg);
myImg.setImageResource = getResources().getDrawable(R.drawable.icon);
0
我保存有通過點擊顯示的圖像的IDS int數組中的onClick(),所以我創建了一個類似於解決機智的解決方法那個問題。
我的onClick()現在看起來是這樣的:
public void showPictureZoomDialog(View view)
{
int singlePicValue400 = 0;
switch (view.getId())
{
case R.id.picture_imgvw_pic1:
singlePicValue400 = currentPicSite.getPicValues400()[0];
break;
case R.id.picture_imgvw_pic2:
singlePicValue400 = currentPicSite.getPicValues400()[1];
break;
case R.id.picture_imgvw_pic3:
singlePicValue400 = currentPicSite.getPicValues400()[2];
break;
case R.id.picture_imgvw_pic4:
singlePicValue400 = currentPicSite.getPicValues400()[3];
break;
case R.id.picture_imgvw_pic5:
singlePicValue400 = currentPicSite.getPicValues400()[4];
break;
case R.id.picture_imgvw_pic6:
singlePicValue400 = currentPicSite.getPicValues400()[5];
break;
case R.id.picture_imgvw_pic7:
singlePicValue400 = currentPicSite.getPicValues400()[6];
break;
case R.id.picture_imgvw_pic8:
singlePicValue400 = currentPicSite.getPicValues400()[7];
break;
case R.id.picture_imgvw_pic9:
singlePicValue400 = currentPicSite.getPicValues400()[8];
break;
case R.id.picture_imgvw_pic10:
singlePicValue400 = currentPicSite.getPicValues400()[9];
break;
}
Intent intent = new Intent(this, PictureZoomDialog.class);
intent.putExtra("PICID", singlePicValue400);
startActivity(intent);
}
我獲得圖像的標識我想通過意圖顯示:
public class PictureZoomDialog extends Activity {
private ImageView mDialog;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture_zoom_dialog);
mDialog = (ImageView)findViewById(R.id.pictureactivity_imgvw_pic1);
mDialog.setClickable(true);
mDialog.setImageResource(getIntent().getIntExtra("PICID",-1));
mDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
THX很多傢伙讓我在正確的方向!
相關問題
- 1. 在android中動態獲取R.drawable ID
- 2. 在jQuery中獲取JSON onClick
- 3. 如何在DatePickerDialog.OnDateSetListener中獲取OnClick?
- 4. 獲取類的onClick onClick
- 5. Java獲取textField.getText()onclick
- 6. R.drawable被替換android.R.drawable
- 7. 在JNI中獲取一個int數組
- 8. Android - 使用int值0作爲「null」 - 等價於R.drawable
- 9. 獲取INT收集
- 10. 獲取int值EditText
- 11. Javascript onClick獲取元素
- 12. jQuery:獲取內容/ innerhtml onclick
- 13. YUI按鈕onclick獲取值
- 14. dc.js - onClick獲取關鍵值
- 15. Android onClick未獲取視圖
- 16. 用jQuery獲取「onclick」的值?
- 17. 從ImageButton獲取CommandArgument onClick,VB
- 18. 從onClick(this)中獲取此ID的ID?
- 19. ActionBarDrawerToggle不接受R.drawable文件
- 20. 如何獲取ViewPager指標OnClick在Android中
- 21. onclick我想要獲取JFrame中存在的JPanel的名稱
- 22. PHP從mysql獲取int值
- 23. 從edittext獲取int的值
- 24. 如何從Iterator獲取int []?
- 25. 獲取枚舉的INT值[]
- 26. 從mysql_fetch_array獲取行號(int)
- 27. 從int獲取顏色
- 28. 從JSON文件獲取int
- 29. 從對象中獲取int值
- 30. Python:從字符串中獲取int值
你真的想確定哪個'ImageView'被點擊或者你真的需要知道'ImageView'包含哪個'Drawable'?你的問題不清楚。 – Squonk 2014-11-09 00:27:13