美文网首页
Android 页面跳转(无/含有返回结果)

Android 页面跳转(无/含有返回结果)

作者: CrazyBoomer | 来源:发表于2017-02-24 19:28 被阅读0次

一.两个页面的xml文件

factivity

    <Button android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="直接跳转" />

    <Button android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转至改变textview" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="回传的数据显示" />

sactivity

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回FA" />

二.在manifest中注册Activity,为首先启动的activity提供标注

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <activity
            android:name=".Factivity"
            android:label="@string/app_name" >
             <intent-filter>//首先启动的activity的标注
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
        </activity>
       

         <activity>
            android:name=".Sactivity"
            android:label="@string/app_name" >
        </activity>
        </application>

三.第一个种跳转:无返回结果的跳转
Factivity文件编写

        private Button bt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.factivity);
        bt1 = (Button) findViewById(R.id.button1);
        bt1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Factivity.this, Sactivity.class);
                startActivity(intent);
            }

此时在模拟器中测试,点击“直接跳转”可以转致另一个页面

四.第二个种跳转:含有返回结果的跳转
Factivity文件编写

    private Button bt2;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.factivity);
        bt2 = (Button) findViewById(R.id.button2);
        tv= (TextView) findViewById(R.id.textView1);
        bt2.setOnClickListener(new OnClickListener() {      
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Factivity.this, Sactivity.class);
                startActivityForResult(intent, 1);          
            }
        });
    }
        @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==2){
            String content = data.getStringExtra("data");
            tv.setText(content);        
        }
    }

Sactivity文件编写

    private Button bt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sactivity);
        bt= (Button) findViewById(R.id.button);
        bt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent data= new Intent();
                data.putExtra("data","hello");
                setResult(2,data);
                finish();
            }
        });
    }

此时点击“跳转至改变textview”可跳转至另一页面,点击另一页面的按钮返回原页面,可见textview已经被修改为“hello”

五.代码的具体理解

Intent intent = new Intent(Factivity.this, Sactivity.class);
startActivity(intent);

可以认为intent是startActivity的一个指引,表达从什么地方(Factivity.this)到什么地方(Sactivity.class)

Intent intent = new Intent(Factivity.this, Sactivity.class);
startActivityForResult(intent, 1);

startActivityForResult中的intent同理,“1”代表了从Factivity到Sactivity的申请代号

Intent data= new Intent();
data.putExtra("data","hello");
setResult(2,data);
finish();

intent不再代表跳转,而是作为信使携带返回参数的代号“data”和具体参数“hello”
setResult()方法中的2是从Sactivity到Factivity的结果代号

if(requestCode==1&&resultCode==2){
String content = data.getStringExtra("data");
tv.setText(content);}

在验证申请代号结果代号之后,初始化一个String通过intent的getStringExtra方法用返回参数的代号“data”得到返回的具体参数

相关文章

网友评论

      本文标题:Android 页面跳转(无/含有返回结果)

      本文链接:https://www.haomeiwen.com/subject/vjobwttx.html