您的位置:首页 > 其它

第三节--通过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>


效果显示:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐