Activity phone screen can be seen as one of the pages ( personal understanding ) is often encountered in development with data from an Activity to another Activity, done Web development knows that in order to pass parameters from a html page to another page is the easiest url = "http: \ \ www.google.com.hk?parmter=xxx". Here 's a look at Android's Activity parameter passing between .
first create a activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/txtMsg"
android:layout_width="fill_parent"
android:layout_height="50px"
android:text="将要传递的内容"
/>
<Button
android:id="@+id/btnsend"
android:layout_width="fill_parent"
android:layout_height="50px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:text="跳转" />
</RelativeLayout>
jump in MainActivity which add logic code
private Button btnsend;
private EditText txtMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMsg = (EditText) this.findViewById(R.id.txtMsg); //获取到文本编辑控件
btnsend=(Button)this.findViewById(R.id.btnsend); //获取到文本编辑控件
btnsend.setOnClickListener(new View.OnClickListener() { //按钮监听事件
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,OtherOneActivity.class);
intent.putExtra("name",txtMsg.getText().toString()); //设置要发送的数据
startActivity(intent);
}
});
}
finally built a OtherOneActivity inheritance Activity, used to receive data
private TextView labmsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
labmsg=new TextView(this);
setContentView(labmsg);
Intent intent=this.getIntent();
String msg=intent.getStringExtra("name");
labmsg.setText(msg);
}
Special Note: Be sure to register in AndroidManifest.xml new Activity
<activity android:name=".OtherOneActivity" android:label="OtherOneActivity"></activity>
没有评论:
发表评论