2016-10-04 56 views
0

你能幫我在onClick位置找到TextView這樣我可以做的東西像setText,在MainActivity如何找到自定義的GridView裏面的觀點來改變他們

動畫應用,改變背景等

我的GridView

final GridView gv = (GridView) findViewById(R.id.gridView1); 
    gv.setAdapter(g); 
    gv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      //TODO: IN HERE I WANT TO CHANGE TEXT, APPLY ANIMATION,AND CHANGE COLOR OF TEXTVIEW PRESENT IN CUSTOM LAYOUT 
      // SO I WANT TO FIND THE TEXTVIEW AS WE NORMALLY DO WITH findViewById(); 
     } 
    }); 

我的自定義佈局我該怎麼找TextView atomicNumber,以便可以應用更改文本和其他東西。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="50dip" 
android:layout_height="50dip" 
android:orientation="vertical"> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:text="1" 
    android:textSize="15dip" 
    android:id="@+id/atomicNumber" 
    android:gravity="right" 
    android:layout_marginTop="5dip"/> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:text="H" 
    android:textSize="20dip" 
    android:id="@+id/elementSymbol" 
    android:gravity="center"/> 
</LinearLayout> 

我試圖找到解決辦法,但無法找到一個

+0

你想從自定義視圖中找到什麼? –

回答

1

你能得到它,你onItemClickListener內。這裏你去:

gv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      //TODO: IN HERE I WANT TO CHANGE TEXT, APPLY ANIMATION,AND CHANGE COLOR OF TEXTVIEW PRESENT IN CUSTOM LAYOUT 
      // SO I WANT TO FIND THE TEXTVIEW AS WE NORMALLY DO WITH findViewById(); 

     // You can use view object to apply findviewbyId method    
     TextView myTextView = (TextView) view.findViewById(R.id.yourTextview); 

     } 
    }); 
+0

謝謝兄弟@jai –

+0

兄弟你可以添加甚至如何找到onItemClick方法之外的視圖。在主要活動的其他方法中處理視圖 –

相關問題