/** 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);
}
Run Code Online (Sandbox Code Playgroud)
在线
EditText editText = (EditText) findViewById(R.id.edit_message);
Run Code Online (Sandbox Code Playgroud)
EditText是类,
editText是我们正在创建的实例.
findViewById(R.id.edit_message)是方法,R.id.edit_message是我们传递的论据
但我不明白为什么会有(EditText)礼物?它是对构造函数的调用吗?
这是明确的类型转换.findViewById()返回a View并(EditText)显式地将其转换为a EditText(它的子类View).这是有效的,因为实际返回的对象EditText是-a,即该类的对象或其子类之一.如果不是,你会得到ClassCastException.
阅读更多:http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
| 归档时间: |
|
| 查看次数: |
7731 次 |
| 最近记录: |