第三节--通过Intent实现Activity的数据传送及Meun的创建和使用
2011-03-14 10:13
639 查看
Menu:
android.view.Menu |
Known Indirect Subclasses ContextMenu, SubMenu |
Interface for managing the items in a menu. By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating an XML file into the Menu via MenuInflater. The easiest way of attaching code to clicks is via onOptionsItemSelected(MenuItem) and onContextItemSelected(MenuItem).
译文:
Menu是一个接口,管理着菜单中的选项。默认情况下,每一个Activity都支持的一个可选菜单活动或选项菜单。您可以添加一个选项到该菜单和给它添加点击功能。添加菜单项的最简单的方法是通过解析xml文件。通过代码实现的最简单方式是添加onOptionsItemSelected(菜单项)和onContextItemSelected(菜单项)。
代码示例:
主Activity /* * 添加布局控件,添加监听并提交数据信息 * author:kevin */ public class Activity_Three extends Activity { private EditText firstOne ; private Button handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根据控件的ID来取得代表控件的对象 firstOne = (EditText)findViewById(R.id.firstOne); handler = (Button)findViewById(R.id.handler); //将监听器的对象绑定到按钮对象上面 handler.setOnClickListener(new HandlerListener()); } //当客户点击MENU按钮的时候,调用该方法 @Override public boolean onCreateOptionsMenu(Menu menu) { /* * 参数1. groupId The group identifier that this item should be part of. * 参数2. itemId Unique item ID. Use NONE if you do not need a unique ID. * 参数3. order The order for the item. * 参数4. title The text to display for the item. */ menu.add(0, 1, 1, R.string.exit); menu.add(0,2,2,R.string.about); return super.onCreateOptionsMenu(menu); } //当客户点击菜单当中的某一个选项时,会调用该方法 @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == 1){ finish(); } return super.onOptionsItemSelected(item); } class HandlerListener implements OnClickListener{ @Override public void onClick(View v) { //取得两个EditText控件的值 String firstMsg= firstOne.getText().toString(); //将值存放到Intent对象当中 Intent intent = new Intent(); intent.putExtra("one",firstMsg); intent.setClass(Activity_Three.this, ResultActivity.class); //启动ResultActivity startActivity(intent); } } }
接收数据Activity /* * 接收主Activity传过来的数据并显示 */ public class ResultActivity extends Activity{ private TextView resultView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.result); resultView = (TextView)findViewById(R.id.result); //得到Intent对象当中的值 Intent intent = getIntent(); String fetchMsg = intent.getStringExtra("one"); int OneIntValue = Integer.parseInt(fetchMsg); //获取所得的值 int result = OneIntValue; resultView.setText("获取所得的值:"+result); } }
布局文件:main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/firstOne" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/alert_info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击下面按钮" /> <Button android:id="@+id/handler" android:text="确定提交" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
布局文件result.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
效果显示:
相关文章推荐
- android中使用intent来实现Activity带数据跳转
- android传递数据方式4--通过Intent实现Activity之间的数据传递
- Android 使用Intent实现Activity跳转和数据传递
- 【Android】使用Intent实现Activity之间的跳转(创建一个Activity的过程)
- android中使用intent来实现Activity带数据跳转
- 使用Intent实现activity之间的数据传递
- 使用js函数实现的通过输入框中数据的长度来控制光标聚焦位置
- 使用Intent在activity之间传递数据
- 通过模仿AsyncTask的封装方式,实现一个后台预读数据的线程,(使用AsyncTask有导致应用FC的风险)
- Android: 用Intent实现Activity之间数据传递
- Android开发之Fragment与Activity的数据交互通过回调机制实现(源代码分享)
- 项目中遇到通过使用路由策略实现主从数据库访问数据不同步的问题
- 利用Intent使Activity和Service通过BroadcastReceiver进行数据交互
- Android 使用intent.putExtra实现Activity之间的参数传递
- Activity之间使用intent传递大量数据带来问题总结
- Oracle 11g 通过创建物化视图实现不同数据库间的表数据同步
- 入门学习3: Intent的使用和在activity间传数据
- Greendao 简单实现增删改查使用过GreenDao的同学都知道,3.0之前需要通过新建GreenDaoGenerator工程生成Java数据对象(实体)和DAO对象,非常的繁琐而且也加大了使用成
- Android判断网络状态是否断开+Android完全关闭应用程序+ 本文讲述了Android中不同Activity之间的数据传递 — Bundle对象的实现(-)+Android中Bundle的使用示例
- android学习笔记---1.为应用添加新的Activity,中使用Intent(意图),结果码,请求码,得到新打开Activity 关闭后返回的数据,Bundle类的作用