安卓android開發筆記2

安卓開發學習中的筆記,遇到什麼寫什麼

1. 獲取view的子view

public void onClick(View v) {
LinearLayout ll = (LinearLayout) findViewById(v.getId()); //最外面那層,也就是v
 RelativeLayout rl = (RelativeLayout) ll.getChildAt(0); //v內部的RelativeLayout 
TextView tv = (TextView) rl.getChildAt(0);//RelativeLayout 內部的TextView
}

要驗證對不對:

if (tv.getId() == R.id.c501_tag) {  //c501_tag就是我在xml文件中給TextView取的id
                    System.out.println("tv==c501_tag");
                }

結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章