Android 简单EventBus登录界面与传值(粘性事件)

来源:互联网 发布:软件开发 证 编辑:程序博客网 时间:2024/05/21 18:44

展示效果

这里写图片描述

添加EventBus导入依赖

compile 'org.greenrobot:eventbus:3.0.0'
  • 1

主MainActivity方法

public class MainActivity extends AppCompatActivity {    private EditText username,password;    private Button btn_go;    private List<UserEvent> mdata;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mdata=new ArrayList<UserEvent>();        username=(EditText)findViewById(R.id.username);        password=(EditText)findViewById(R.id.passwork);        btn_go=(Button)findViewById(R.id.btn_go);        btn_go.setText("登录");        btn_go.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                String name = username.getText().toString().trim();                String pass = password.getText().toString().trim();                EventBus.getDefault().postSticky(new UserEvent(name,pass));                startActivity(new Intent(MainActivity.this,MainBctivity.class));            }        });    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

主MainBctivity方法

public class MainBctivity extends AppCompatActivity {    private Button btn_shou;    private TextView tv_b;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main_bctivity);        btn_shou=(Button)findViewById(R.id.btn_shou);        btn_shou.setText("接受参数");        btn_shou.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if(!EventBus.getDefault().isRegistered(MainBctivity.this)){                    EventBus.getDefault().register(MainBctivity.this);                }else{                    Toast.makeText(MainBctivity.this, "请勿重复注册事件", Toast.LENGTH_SHORT).show();                }            }        });        tv_b=(TextView)findViewById(R.id.tv_b);        tv_b.setText("账号多少呢!");    }    @Override    protected void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(MainBctivity.this);    }    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)    public void onMoonEvent(UserEvent userevent){        tv_b.setText("账号:"+userevent.getUsername()+"密码:"+userevent.getPasswork());    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

UserEvent(事件类)

public class UserEvent {    private String username;    private String passwork;    public UserEvent(String username, String passwork) {        this.username = username;        this.passwork = passwork;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPasswork() {        return passwork;    }    public void setPasswork(String passwork) {        this.passwork = passwork;    }    public UserEvent() {    }    @Override    public String toString() {        return "UserEvent{" +                "username='" + username + '\'' +                ", passwork='" + passwork + '\'' +                '}';    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

activity_main(MainActivity的布局)

<RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <ImageView            android:id="@+id/hh_img"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@mipmap/logo"            android:layout_centerHorizontal="true"            android:layout_marginTop="40dp"            />        <EditText            android:id="@+id/username"            android:layout_below="@id/hh_img"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:hint="用户名"            />        <EditText            android:id="@+id/passwork"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/username"            android:layout_marginTop="10dp"            android:hint="密码"            />        <Button            android:id="@+id/btn_go"            android:layout_below="@id/passwork"            android:layout_marginTop="10dp"            android:layout_width="match_parent"            android:layout_height="wrap_content"            />        <TextView            android:id="@+id/new_user"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@id/btn_go"            android:text="新用户"            android:layout_marginTop="5px"            />    </RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

activity_main_bctivity(MainBctivity的布局)

<Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="20dp"        android:id="@+id/btn_shou" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/btn_shou"        android:layout_centerHorizontal="true"        android:layout_marginTop="32dp"        android:id="@+id/tv_b" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

谢谢观看,小编祝大家生活愉快!(多多关注小编,会有非常之多精彩分享哦!)

            <link rel="stylesheet" href="http://s.csdnimg.cn/static/production/markdown_views-d4dade9c33.css">                </div>
原创粉丝点击