2015-02-24 118 views
0

我想設置運行時間id並獲取id。 我的代碼是...Android如何設置運行時間Id

TextView image=new TextView(this); 
      LinearLayout.LayoutParams imageParams = new  LinearLayout.LayoutParams(100,100); 
      image.setLayoutParams(imageParams); 
       image.setBackgroundResource(R.drawable.grey); 
      image.setId(1); 
    image.setOnclickListner(this); 

但是當我設置image.setId(1),然後給出錯誤在Android Studio中。 然後如何在視圖下查找圖像ID ..

+0

什麼是錯誤你得到 – koutuk 2015-02-24 11:22:49

+0

誤差預計ID類型的資源 – Destro 2015-02-24 11:28:10

+0

#Destro我們的代碼是絕對被罰款......請其他人導致此錯誤 – koutuk 2015-02-24 11:29:57

回答

0

在以下API級別17中,不可能生成運行時ID ..如果我們生成id,則我們將Tag設置爲image.setTag(1)。例如..

TextView image=new TextView(this); 
     LinearLayout.LayoutParams imageParams = new  LinearLayout.LayoutParams(100,100); 
     image.setLayoutParams(imageParams); 
      image.setBackgroundResource(R.drawable.grey); 
     image.setTag(1); 
    image.setOnclickListner(this); 
} 

和獲取標記這樣

@Override 
public void onClick(View v) { 
    Log.e("hello",""+v.getTag()); 

}