2017-07-16 63 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