/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
在行中
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText
是类并且
editText
是我们正在创建的实例。
findViewById(R.id.edit_message)
是方法,R.id.edit_message
是我们传递的参数
但我不明白为什么会有 (EditText)
存在?
是对构造函数的调用吗?
最佳答案
这是显式类型转换。 findViewById()
返回一个 View
并且 (EditText)
明确地将其类型转换为一个 EditText
(它是一个子类查看
)。这是可行的,因为返回的对象实际上是一个 EditText
,即该类或其子类之一的对象。如果不是,您将得到 ClassCastException
。
阅读更多:http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
关于java - 解释语法 EditText editText = (EditText) findViewById(R.id.edit_message);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21879232/