2011-11-25 30 views
2

如何在scrollView中找到此元素,如何獲取已被單擊的元素的ID?在我的情況下,我有4張照片(ImageViews),如果我點擊其中的一張,我想要得到它的ID(已經用ImageView.setId(int)設置)。這裏是我的代碼:在Android的滾動視圖中獲取ImageView的ID

package scroll.s; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.HorizontalScrollView; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class ScrollsActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     int layoutId = 0; 

     RelativeLayout main = new RelativeLayout(this); 
     RelativeLayout.LayoutParams mainParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     main.setId(layoutId); 
     layoutId++; 

     TextView past = new TextView (this); 
     RelativeLayout.LayoutParams pastParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     past.setId(layoutId); 
     layoutId++; 
     past.setText("Past: "); 
     pastParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     main.addView(past, pastParams); 

     final HorizontalScrollView hsv = new HorizontalScrollView(this); 
     hsv.setId(layoutId); 
     layoutId++; 
     hsv.setHorizontalScrollBarEnabled(false); 
     mainParams.addRule(RelativeLayout.BELOW, 1); 
     main.addView(hsv, mainParams);   

     final RelativeLayout relative1 = new RelativeLayout(this); 
     relative1.setId(layoutId); 
     layoutId++; 
     hsv.addView(relative1); 

     for (int i=0; i<4; i++) { 
      ImageView current = new ImageView(this); 
      current.setBackgroundDrawable(getResources().getDrawable(R.drawable.example)); 
      current.setId(layoutId); 

      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      params.addRule(RelativeLayout.RIGHT_OF, (layoutId-1)); 
      params.leftMargin=10; 
      relative1.addView(current, params); 
      layoutId++; 
     } 

     for (int i=0; i<4; i++) { 
      TextView currentText = new TextView(this); 
      currentText.setText("random text"); 
      currentText.setId(layoutId); 

      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      params.addRule(RelativeLayout.BELOW, (layoutId-4)); 
      params.addRule(RelativeLayout.ALIGN_LEFT, (layoutId-4)); 
      params.topMargin=5; 
      relative1.addView(currentText, params); 
      layoutId++; 
     } 


     this.setContentView(main); 
    } 
} 

這是我這段代碼實現:scrollsview

我得到ERROR/AndroidRuntime(2258):android.content.res.Resources $ NotFoundException:字符串資源ID #爲0x4 當我把下面的代碼:

current.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getBaseContext(), current.getId(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 

回答

2

將其更改爲:

current.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getBaseContext(), String.valueOf(v.getId()), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
+2

如果你傳遞一個int makeText的第二個參數,它假定它是strings.xml文檔中列出的String的id。 – FunkTheMonk

+0

感謝隊友,工作! –

1

方法makeText()的第二個參數是一個字符串,不是詮釋current.getId()),所以你應該把它轉換爲String這樣的:

current.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(getBaseContext(),""+v.getId(), Toast.LENGTH_SHORT).show(); 
      } 
     });