Android實現(xiàn)EventBus登錄界面與傳值(粘性事件)
本文實例為大家分享了Android實現(xiàn)EventBus登錄界面與傳值的具體代碼,供大家參考,具體內(nèi)容如下
展示效果

添加EventBus導入依賴
compile 'org.greenrobot:eventbus:3.0.0'
主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));
}
});
}
}
主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("接受參數(shù)");
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的布局)
<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>
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" />
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android視頻/音頻緩存框架AndroidVideoCache(Okhttp)詳解
這篇文章主要為大家詳細介紹了Android視頻、音頻緩存框架AndroidVideoCache,實現(xiàn)邊下邊播功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Android OKHttp框架的分發(fā)器與攔截器源碼刨析
okhttp是一個第三方類庫,用于android中請求網(wǎng)絡。這是一個開源項目,是安卓端最火熱的輕量級框架,由移動支付Square公司貢獻(該公司還貢獻了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient2022-11-11
Android開發(fā)InputManagerService創(chuàng)建與啟動流程
這篇文章主要為大家介紹了Android開發(fā)InputManagerService創(chuàng)建與啟動流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android中TextView限制最大行數(shù)并在最后用顯示...全文
TextView在android開發(fā)中是一個經(jīng)常用到的基礎控件,功能也很強大,限制輸入字符類型,字數(shù)什么的,下面這篇文章主要給大家介紹了關于Android中TextView限制最大行數(shù)并在最后用顯示...全文的相關資料,需要的朋友可以參考下2022-04-04
android如何取得本地通訊錄的頭像的原圖的實現(xiàn)代碼
這篇文章主要介紹了android如何取得本地通訊錄的頭像的原圖的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12

