2017-07-16 69 views
-2

我對findviewbyid有點困惑。只是想給出一個簡短的解釋,如果有任何錯誤,請檢閱它們。findviewbyid函數怎麼樣

class test 
    { 
    Button btes; 
    public void OnCreate(Bundle savedIntasnceState); 
    super.OnCreate(savedInstanceState); 
    btes = (Button)findViewById(R.id.button); 

    } 

這是否意味着findViewById返回對Android.Widget.Button的View對象和實例的引用。 Android.widget.button擴展了findViewById。

問題: 1.上述說明是否正確。如果不是,那麼正確的說法是什麼。 2.任何人都可以提供視圖類的findViewById函數的源代碼 3.什麼是R.java中的那些十六進制數字。 a)

+1

有沒有必要要求在SO中定義一種方法。你可以去[developer.android.com](https://developer.android.com/reference/android/app/Activity.html#findViewById(int))來學習這些。您也可以按ctrl +右鍵單擊該方法以查看Android Studio中的方法實現和定義。 – Blasanka

回答

0

1a。沒有你說的聲明是不正確的,正確的版本將如下:

findViewById(R.id.button)是一種方法,它用於查找您在XML中使用標記android:id設置的視圖; (button)是將返回值轉換爲按鈕對象的類型

1b。按鈕TextView的擴展延伸查看

2.請參見下面

3.請說明你的意思(添加到您的問題,評論)

Learn more about findViewById here

Learn more about type casting here

+0

感謝您的回答,我有一個疑問,只有在返回對象是從繼承視圖類的按鈕類實例化的時候才能完成類型轉換。例子查看something = new Button(); –

+0

那麼,如果你要做到以下幾點:查看foo = new Button();由於Button類擴展了擴展View的TextView,因此您可能會** Downcasting或Narrowing Reference Conversion **。基本上是將一堆額外的數據(所有使按鈕成爲按鈕的東西)切斷,直到只剩下一個View。你應該檢查這個[post](https://stackoverflow.com/a/20097325/3843432)以獲取更多關於類型轉換的信息。 – Seabass77