本文实例为大家分享了Android实现EventBus登录界面与传值的具体代码,供大家参考,具体内容如下
成都创新互联公司致力于互联网网站建设与网站营销,提供成都网站设计、做网站、网站开发、seo优化、网站排名、互联网营销、微信小程序定制开发、公众号商城、等建站开发,成都创新互联公司网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。
展示效果
添加EventBus导入依赖
compile 'org.greenrobot:eventbus:3.0.0'
主MainActivity方法
public class MainActivity extends AppCompatActivity { private EditText username,password; private Button btn_go; private Listmdata; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mdata=new ArrayList (); 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)); } }); } }
主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()); } }
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 + '\'' + '}'; } }
activity_main(MainActivity的布局)
activity_main_bctivity(MainBctivity的布局)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。