0

私の英語でごめんなさい!次のようなコードがあります。

public void onClick(View v) {
    String imgName="";
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:{
        Random rand = new Random();
        int rndInt = rand.nextInt(24) + 1;
        imgName = "img" + rndInt;
        int id = getResources().getIdentifier(imgName, "drawable", getPackageName());  
        imageView1.setImageResource(id);

        break;}

    default:
        break;
    };

}

ランド画像の画像の説明をクリックして、新しい意図を設定するにはどうすればよいですか? それぞれに個別?サンプル: ボタンをクリックすると、ランド イメージが魚になります。それをクリックすると、text=Fish can swim! で新しいインテントが開きます。または、ランド画像は猫です。クリックすると、新しいインテントが開きます。

4

1 に答える 1

1

まず、 の を作成onTouchlistenerしますimageView。次に、ACTION_DOWNイベントでどのランダムimageResourceが設定されているかを確認しimageView、インテントを実行します。

画像リソース名を取得する場合は、次を使用できます。

String imageName = (String) imageView.getTag();

ここで、カスタム テキストを使用して新しいアクティビティを開始するには:

if(imageName.equalsIgnoreCase("fish"))
   description="Fish lives in water..."
else if(imageName.equalsIgnoreCase("cat"))
   description="cat is cute..."
.
.
.
.

すべてが比較された後、アクティビティを呼び出し、

Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra(description,"ID");
startActivity(i);

2 番目のアクティビティでは、画像の説明を取得します。

Bundle extras = getIntent().getExtras();
String description = extras.getString("ID");
于 2012-08-02T03:13:03.813 に答える