如何在android中獲取動態創建的View的ID?其實我在我的佈局中有很多視圖,我想要用戶觸摸的那個視圖的id。它有可能嗎?如果是,比如何做呢?如何在android中動態獲取ViewId
在下面的代碼中創建了新的框架佈局,我爲此佈局生成了ViewViewId並將此ID保存爲HashMap(logoResizingRecordList)的鍵。
FrameLayout fm=new FrameLayout(getApplication());
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(400,300);
fm.addView(img_pinch,params);
int FrameLayoutID=fm.generateViewId();
logoResizingRecordList.put(FrameLayoutID,logoObj);
fm.setOnTouchListener(logoTouchListner);
在這裏OnTouch我已經將FrameLayout的實例分配給temp_img_logo。
public boolean onTouch(final View p_v, MotionEvent p_event)
{
if(p_v instanceof FrameLayout) {
temp_img_logo=(FrameLayout)p_v;
}
在這裏我要得到上面的FrameLayout(FM)的ID,但它返回任何搜索即-1的默認標識
logoResizingRecordList.get(temp_img_logo.getId())
現在我的問題是有可能得到我已經動態創建的fm(FrameLayout)的ID?如果是,那麼請幫我找到它。
你所說的「動態創建」呢?你的意思是你在'View'子類中使用構造函數(例如'new Button(...)')?你的意思是你使用'LayoutInflater'?你的意思是別的嗎? – CommonsWare 2015-04-05 12:22:20
@CommosWare動態表示TextView以編程方式創建,然後將其添加到佈局中。現在我想在OnTouch事件中獲取此Textview的ID。 – NighterZ 2015-04-05 12:25:42
你是什麼意思「以編程方式創建」?你的意思是你在'View'子類中使用構造函數(例如'new Button(...)')?你的意思是你使用'LayoutInflater'?你的意思是別的嗎? – CommonsWare 2015-04-05 12:27:45