Android_Intent Demo(acticity1与activity2相互跳转)

Main.activity:

package com.example.intentdemo2;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn;
    private static final int REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button) findViewById(R.id.myButton1);
btn.setOnClickListener(new OnClickListener(){

public void onClick(View arg0){
Intent intent=new Intent();
intent.setClass(MainActivity.this, secondActivity.class);
intent.putExtra("str", "com from first activity");
startActivityForResult(intent,REQUEST_CODE);

}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE){
if(resultCode==secondActivity.RESULT_CODE){
Bundle bundle=data.getExtras();
String str = bundle.getString("back");
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();

}

}

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


secondActivity:

package com.example.intentdemo2;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
public class secondActivity extends Activity {
private TextView tv;
private Button btn2;
public static final int RESULT_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String str=bundle.getString("str");
tv=(TextView) findViewById(R.id.myTextView1);
tv.setText(str);
btn2=(Button) findViewById(R.id.myButton2);
btn2.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
Intent intent=new Intent();
intent.putExtra("back", "come from second activity");
setResult(RESULT_CODE,intent);
                finish();
}
});
}
}

main.xml:

<LinearLayout 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:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第一个 activity" />
    <Button
        android:id="@+id/myButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"/>
</LinearLayout>


second.xml

<?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" >    
    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="第二个 activity" />
    <Button
        android:id="@+id/myButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second"/>
</LinearLayout>


在 AndroidManifest.xml中添加 <activity android:name=".secondActivity" android:label="Second Activity"></activity>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值