在学习 Android 教程时,我遇到了一些我不明白的事情。这可能非常简单,但我只需要知道为什么会这样。
教程中:http://developer.android.com/resources/tutorials/views/hello-autocomplete.html
本教程似乎使用以下方法构建了一个新的 AutoCompleteTextView:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
我假设他们使用构造函数:
AutoCompleteTextView(Context context, AttributeSet attrs)
我认为他们的 AttributeSet 是“findViewById(R.id.autocomplete_country)
”;而它们的上下文是 (AutoCompleteTextView)
。这样对吗?
另外... new 关键字在哪里,逗号,为什么有一对括号?
我一直认为它必须是:
AutoCompleteTextView textview = new AutoCompleteTextView(context here, attrs here);
我哪里错了?!
最佳答案
findViewById
返回与传入参数对应的 View
,然后您将View
转换为您正在使用的任何类型的对象。
Button myButton = (Button) findViewById(R.id.button);
^ casting ^ returns the View of your button.
关于java - "new"关键字在哪里? Android教程困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2394723/