Android如何设置运行时间Id

Android how to set run time Id

我想设置运行时间id并获取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..

在 API 级别 17 下不可能生成 运行 时间 ID.. 如果我们生成 ID 然后我们将标签设置为 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());

}