Android實(shí)現(xiàn)EventBus登錄界面與傳值(粘性事件)

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

成都創(chuàng)新互聯(lián)公司致力于互聯(lián)網(wǎng)網(wǎng)站建設(shè)與網(wǎng)站營(yíng)銷,提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站開發(fā)、seo優(yōu)化、網(wǎng)站排名、互聯(lián)網(wǎng)營(yíng)銷、微信小程序定制開發(fā)、公眾號(hào)商城、等建站開發(fā),成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)策劃專家,為不同類型的客戶提供良好的互聯(lián)網(wǎng)應(yīng)用定制解決方案,幫助客戶在新的全球化互聯(lián)網(wǎng)環(huán)境中保持優(yōu)勢(shì)。

展示效果

Android實(shí)現(xiàn)EventBus登錄界面與傳值(粘性事件)

添加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, "請(qǐng)勿重復(fù)注冊(cè)事件", Toast.LENGTH_SHORT).show();
  }
  }
 });
 tv_b=(TextView)findViewById(R.id.tv_b);
 tv_b.setText("賬號(hào)多少呢!");

 }

 @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("賬號(hào):"+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" />

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文題目:Android實(shí)現(xiàn)EventBus登錄界面與傳值(粘性事件)
標(biāo)題網(wǎng)址:http://bm7419.com/article40/igcieo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、定制開發(fā)ChatGPT、響應(yīng)式網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)