android:Intent的两种跳转

重新写安卓程序,居然忘记Intent的用法,重新回顾一下,加深一下影响。
在安卓里面编程比较常用的跳转那就是Intent。Intent的意思是意图,Intent跳转有两种方式,一种就是没有返回值的,一种是有返回值的。
没有返回值的用法:
1、创建一个intent,然后把需要的参数传进去。

Intent intent=new Intent(this, SActivity.class);

一个是Context参数,一个是要跳转的页面。
2、调用方法startActivity去跳转。

startActivity(intent);

有返回值的用法:
1、创建一个intent,然后把需要的参数传进去。

Intent intent2=new Intent(this, SActivity.class);

2、调用方法startActivityForResult(Intent intent, int requestCode)

startActivityForResult(intent2, 1);

3、使用有返回值的那就需要去处理这个返回值,那就需要去重写onActivityResult方法。
onActivityResult(int requestCode, int resultCode, Intent data)

  • requestCode :这个是请求码
  • resultCode: 这个是结果码
  • data:Intent实例

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==1&&resultCode==2){
String string = data.getStringExtra(“data”);
mTextView.setText(string);
}
}

4、跳转后的页面需要处理 setResult(int resultCode, Intent data)方法。

setResult(2, intent);

最后代码呈上:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class FActivity extends Activity implements OnClickListener{
    private Button mButton;
    private Button mButton2;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_f);
        mTextView =(TextView) findViewById( R.id.textView1);
        mButton=(Button) findViewById(R.id.button1);
        mButton2=(Button) findViewById(R.id.button2);
        mButton.setOnClickListener(this);
        mButton2.setOnClickListener(this);


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1&&resultCode==2){
            String string = data.getStringExtra("data");
            mTextView.setText(string);
        }
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:
            Intent intent=new Intent(FActivity.this, SActivity.class);
            startActivity(intent);
            break;
        case R.id.button2:
            Intent intent2=new Intent(FActivity.this, SActivity.class);
            startActivityForResult(intent2, 1);
            break;  
        default:
            break;
        }
    }
}


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SActivity extends Activity implements OnClickListener{
    private Button mButton;
    String data="你好";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s);
        mButton =(Button) findViewById(R.id.button3);
        mButton .setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent=new Intent();
        intent.putExtra("data", data);
        //添加数据,然后回传给之前的页面
        setResult(2, intent);
        finish();
    }

}

activity_f代码:
<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"
    tools:context=".FActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/hello_world" />

</RelativeLayout>

activity_s代码:
<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"
    tools:context=".SActivity" >

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

总结完了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值