2011-11-25 69 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(); 
      } 
     });