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

添加EventBus導(dǎo)入依賴
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, "請勿重復(fù)注冊事件", 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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 再按一次返回鍵退出程序?qū)崿F(xiàn)思路
用戶退出應(yīng)用前給出一個提示是很有必要的,因為可能是用戶并不真的想退出,而只是一不小心按下了返回鍵,大部分應(yīng)用的做法是在應(yīng)用退出去前給出一個Dialog提示框;個人覺得再按一次返回鍵退出程序很有必要,接下來介紹一些簡單實現(xiàn)2013-01-01
Android Studio如何獲取SQLite數(shù)據(jù)并顯示到ListView上
這篇文章主要介紹了Android Studio獲取SQLite數(shù)據(jù)并顯示到ListView上,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實例詳解
這篇文章主要介紹了Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法,結(jié)合實例形式較為詳細(xì)的分析了WalkLock與KeyguardLock的功能、作用、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-01-01
基于linux與windows平臺下 如何下載android sdk源代碼的方法詳解
本文主要是介紹在linux和windows平臺下,如何下載android sdk的源代碼,注意是sdk的源代碼,而不是android的所有源代碼,同時介紹如何把sdk源代碼加入到eclipse里,使android 平臺手機開發(fā)者可以直接查看源代碼,通過閱讀SDK源碼,能更好的理解和運用Android的API2013-05-05
Android 選擇相冊照片并返回功能的實現(xiàn)代碼
這篇文章主要介紹了Android 從相冊中選擇照片并返回功能,需要的朋友可以參考下2018-03-03
Android實戰(zhàn)RecyclerView頭部尾部添加方法示例
本篇文章主要介紹了Android實戰(zhàn)RecyclerView頭部尾部添加方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android通過startService實現(xiàn)文件批量下載
這篇文章主要為大家詳細(xì)介紹了Android通過startService實現(xiàn)文件批量下載的示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12
Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法
這篇文章主要介紹了Android編程之創(chuàng)建自己的內(nèi)容提供器實現(xiàn)方法,結(jié)合具體實例形式分析了Android創(chuàng)建內(nèi)容提供器的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

