我正在使用一个意图开始另一个活动,并使我的意图携带一些数据作为新创建的活动的额外.我正在按照教程来做到这一点.
实际上,此数据从层次结构中第一个活动的文本字段中读取,并作为额外活动传送到另一个活动.所以我的代码将是:
//Make the intent
Intent intent = new Intent(this, SecondActivity.class);
/* Find the text field (view) which contains the data, and save it into a newly created text field (view of the same type)*/
EditText editText = (EditText) findViewById(R.id.iden1); //******************************
//Read that view's string data into a string called message
String message= editmessage.getText().toString();
//copy this message into the extra named extra_name, of intent.
intent.putExtra(extra_name, message);
Run Code Online (Sandbox Code Playgroud)
我的问题来自这个声明:
EditText editText = (EditText) findViewById(R.id.iden1);
Run Code Online (Sandbox Code Playgroud)
我的问题是显式转换即(EditText).为什么我们将findViewById()返回的EditText视图(在layout.xml中定义并由android:id ="@ + id/iden1"标识)再次转换为EditText视图.此处视图editText的类型和layout.xml中创建的类型相同.那么这种类型铸造有什么意义呢?
关键是findViewById(int id)返回一个通用View对象.此方法不会解析您的xml,以便了解您的视图类型.它只是View根据您的R.java文件建立的关系创建一个新对象,由IDE构建(Eclipse,我想).
您需要转换结果,findViewById(int id)因为您没有转换EditText对象,您的转换视图,其中EditText只有一个规范.
| 归档时间: |
|
| 查看次数: |
3097 次 |
| 最近记录: |