Demo01:Activity的迁移

来源:互联网 发布:淘宝未按约定发布商品 编辑:程序博客网 时间:2024/06/07 20:57
package com.wuqiong.demo01;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class Activity1 extends Activity {private Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.first_layout);button1 = (Button) findViewById(R.id.button_1);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(Activity1.this, Activity2.class);startActivityForResult(intent, 1);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch(requestCode) {case 1:if(resultCode == RESULT_OK) {String returnedData = data.getStringExtra("data_return");Toast.makeText(Activity1.this, returnedData, Toast.LENGTH_SHORT).show();button1.setText(returnedData);}break;default:}}}
</pre><pre name="code" class="java"><pre name="code" class="java">package com.wuqiong.demo01;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Activity2 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second_layout);Button button2 = (Button) findViewById(R.id.button_2);button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.putExtra("data_return", "Activity2 to Activity1");setResult(RESULT_OK, intent);finish();}});}@Overridepublic void onBackPressed() {super.onBackPressed();Intent intent = new Intent();intent.putExtra("data_return", "Activity2 to Activity1");setResult(RESULT_OK, intent);finish();}}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Activity1" /></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" ><Button    android:id="@+id/button_2"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="Activity2" /></LinearLayout>



0 0