美文网首页
Intent 界面跳转和传值

Intent 界面跳转和传值

作者: Luyc_Han | 来源:发表于2017-11-15 18:13 被阅读12次
    第一个页面布局:
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button122"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="第一种" />
    
        <Button
            android:id="@+id/button123"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="第二种"/>
    
        <TextView
            android:id="@+id/textviewaa"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:hint="内容"/>
    
    </LinearLayout>
    
    </android.support.constraint.ConstraintLayout>
    
    第一个界面实现:
    package com.example.xiaojie.paomadeng;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    /**
     * Created by xiaojie on 2017/11/15.
     */
    
    public class FActivity extends Activity {
    
        private TextView textV;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.fayout);
    
    
            Button btn = (Button) findViewById(R.id.button122);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    Intent intent = new Intent(FActivity.this, TActivity.class);
    
                    FActivity.this.startActivity(intent);
    
                }
            });
    
    
    
            Button btn1 = (Button) findViewById(R.id.button123);
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    Intent intent = new Intent(FActivity.this, TActivity.class);
    
                    startActivityForResult(intent,1);
    
                }
            });
    
    
            textV = (TextView)findViewById(R.id.textviewaa);
    
    
    
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            Log.i("tag", "onActivityResult: dddd");
    
            Log.i("tag", "onActivityResult: requestCode:" + requestCode);
    
    
            Log.i("tag", "onActivityResult: resultCode" + resultCode);
    
    
            Log.i("tag", "onActivityResult: data" + data.getStringExtra("name"));
    
            String content = data.getStringExtra("name");
    
            textV.setText(content);
    
        }
    }
    
    
    第二个页面布局:
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <Button
                android:id="@+id/huichuan"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="回传"/>
    
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:hint="回传内容"/>
            
    
        </LinearLayout>
    
    </android.support.constraint.ConstraintLayout>
    
    第二个实现
    package com.example.xiaojie.paomadeng;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.View;
    import android.widget.Button;
    
    /**
     * Created by xiaojie on 2017/11/15.
     */
    
    public class TActivity extends Activity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.tlayout);
    
    
            Button btn1 = (Button) findViewById(R.id.huichuan);
            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    Intent data = new Intent();
    
                    data.putExtra("name", "Mr.H");
    
                    setResult(100, data);
    
                    finish();
    
                }
            });
    
    
        }
    
    
    }
    

    效果图:


    Untitled.gif

    相关文章

      网友评论

          本文标题:Intent 界面跳转和传值

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