android 页面跳转,数据回传
package com.example.firstpg.firstpg; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.Window; import android.widget.Toast; import android.content.Intent; public class FirstlayoutActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //requestWindowFeature(Window.FEATURE_ACTION_BAR); setContentView(R.layout.firstlayout); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_firstlayout, menu); return true ; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement switch (id) { case R.id.add_item: Toast.makeText(this,"大海啊大海",Toast.LENGTH_SHORT).show(); break; case R.id.edit_item: Intent intent=new Intent(FirstlayoutActivity.this,SecondlayoutActivity.class); startActivity(intent); break; case R.id.del_item: Intent intent2=new Intent(FirstlayoutActivity.this,SecondlayoutActivity.class); intent2.putExtra("data1", "你好1"); startActivityForResult(intent2,1); break; default: finish(); } return super.onOptionsItemSelected(item); } protected void onActivityResult(int requestCode,int resultCode,Intent data) { switch (requestCode) { case 1: if (resultCode == RESULT_OK) { String retrundata = data.getStringExtra("data2"); Log.d("data2", retrundata); } break; } } }
package com.example.firstpg.firstpg; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class SecondlayoutActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondlayout); Intent intent=getIntent(); String data=intent.getStringExtra("data1"); Log.d("data1", data); Button button2=(Button)findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intert=new Intent(); intert.putExtra("data2","你好2"); setResult(RESULT_OK,intert); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_secondlayout, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".FirstlayoutActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:layout_centerHorizontal="true" android:layout_marginTop="195dp" /> </RelativeLayout>
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.firstpg.firstpg.SecondlayoutActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".FirstlayoutActivity"> <item android:id="@+id/add_item" android:title="添加" /> <item android:id="@+id/edit_item" android:title="修改" /> <item android:id="@+id/del_item" android:title="回传数据测试" /> <item android:id="@+id/return_item" android:title="退出" /> </menu>