EventBus跳转传值

一,导入依赖

compile 'org.greenrobot:eventbus:3.1.1'
              这里用到一个控件
TextInputLayout
,可以使页面变得美观,也需要导入依赖

compile 'com.android.support:design:23.3.0'

二,布局文件

       1,第一个页面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="Welcome"
            android:textSize="30sp"
            android:textColor="#333333"/>

    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/usernameWrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:hint="Username"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/passwordWrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/usernameWrapper"
            android:layout_marginTop="4dp">

            <EditText
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="Password"/>

        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/btn"
            android:layout_marginTop="4dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Login"/>

    </LinearLayout>
</LinearLayout>
      2,第二个页面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center">
        <Button
            android:id="@+id/button"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="接收数据" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/uname"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/upass"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

三,定义消息事件类,有几个参数写几个参数

    

public class MenegerEvent {
    //定义消息事件类
    public final String name;
    public final String pwd;

    public MenegerEvent(String name, String pwd) {
        this.name = name;
        this.pwd = pwd;
    }
}

四,Activity


//注册和取消订阅事件
public class Main2Activity extends AppCompatActivity {
    private TextView name,pass;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //找到控件
        name= findViewById(R.id.uname);
        pass= findViewById(R.id.upass);
        button= findViewById(R.id.button);
        //点击事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //注册EventBus
                EventBus.getDefault().register(Main2Activity.this);
            }
        });
    }
    //事件订阅者处理事件
    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void onUserEvent(MenegerEvent event) {
        name.setText("用户名:" + event.name);
        pass.setText("用户名:" + event.pwd);

    }

    //取消注册
    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}

五,Avtivity2

//注册和取消订阅事件
public class Main2Activity extends AppCompatActivity {
    private TextView name,pass;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //找到控件
        name= findViewById(R.id.uname);
        pass= findViewById(R.id.upass);
        button= findViewById(R.id.button);
        //点击事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //注册EventBus
                EventBus.getDefault().register(Main2Activity.this);
            }
        });
    }
    //事件订阅者处理事件
    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void onUserEvent(MenegerEvent event) {
        name.setText("用户名:" + event.name);
        pass.setText("用户名:" + event.pwd);

    }

    //取消注册
    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}

             

在uniapp中,页面跳转是一个常见的需求。uniapp提供了多种方式来实现页面之间的数据递。以下是几种常用的方法: 1. **使用navigateTo递参数**: 通过`uni.navigateTo`方法跳转页面时,可以在`url`中拼接参数。 ```javascript // 页面A uni.navigateTo({ url: '/pages/pageB/pageB?name=张三&age=25' }); // 页面B onLoad: function(options) { console.log(options.name); // 输出:张三 console.log(options.age); // 输出:25 } ``` 2. **使用事件总线(EventBus)**: 通过事件总线,可以在不同页面之间递数据。 ```javascript // 创建一个事件总线 const eventBus = new Vue(); // 页面A触发事件 eventBus.$emit('eventName', { name: '张三', age: 25 }); // 页面B监听事件 eventBus.$on('eventName', (data) => { console.log(data.name); // 输出:张三 console.log(data.age); // 输出:25 }); ``` 3. **使用Vuex进行全局状态管理**: 通过Vuex,可以在全局范围内管理状态和数据。 ```javascript // store.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.Store({ state: { name: '', age: 0 }, mutations: { setUser(state, payload) { state.name = payload.name; state.age = payload.age; } } }); export default store; // 页面A store.commit('setUser', { name: '张三', age: 25 }); // 页面B computed: { userName() { return this.$store.state.name; }, userAge() { return this.$store.state.age; } } ``` 4. **使用本地存储(localStorage或uni.setStorage)**: 通过本地存储,可以在页面之间持久化数据。 ```javascript // 页面A uni.setStorageSync('user', { name: '张三', age: 25 }); // 页面B onLoad: function() { const user = uni.getStorageSync('user'); console.log(user.name); // 输出:张三 console.log(user.age); // 输出:25 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值