Android中的eventBus傳值
第一步:在build.gradle中新增依賴 dependencies { compile 'org.greenrobot:eventbus:3.0.0' } 第二步:建立一個 Event類: 注意:enum 不是class public enum Event { //訊息名,可以根據這個名字判斷是哪個訊息 IMAGE_LOADER_SUCCESS; }
//------------主佈局檔案------------------------ <TextView android:layout_width="match_parent" android:layout_height="wrap_content"android:id="@+id/tv_text" /> <Button android:layout_width="match_parent" android:layout_height="40dp" android:text="點選跳轉到TwoActivity" android:id="@+id/bt_button"/> //-----------TwoActivity佈局----------------------- <Button android:layout_width="match_parent" android:layout_height="wrap_content"android:id="@+id/bt_button2" android:text="傳送訊息"/> //---------建立一個訊息了---MessageEvent ------------------------ public class MessageEvent { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public MessageEvent(String message) { this.message = message; } public MessageEvent() { } } //--------------MainActivity -中我們註冊事件,用於接收訊息,退出時需要登出----------------- package eventbusdemo.wode.com.eventbusdemo; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button bt_button; private TextView tv_text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到控制元件 bt_button = (Button) findViewById(R.id.bt_button); tv_text = (TextView) findViewById(R.id.tv_text); //在需要訂閱事件的地方註冊事件 EventBus.getDefault().register(this); //設定監聽 bt_button.setOnClickListener(this); } //用於接收傳MessageEvent這個類的訊息 @Subscribe(threadMode = ThreadMode.MAIN) public void onShowMessageEvent(MessageEvent messageEvent) { tv_text.setText("MainActivity:" + messageEvent.getMessage()); } //用於接收沒有傳值的訊息 /* @Subscribe public void onEventMainThread(Event event){ switch (event){ case IMAGE_LOADER_SUCCESS: Log.i("MainActivity--","IMAGE_LOADER_SUCCESS"); tv_text.setText("IMAGE_LOADER_SUCCESS"); break; } }*/ @Override public void onClick(View v) { switch (v.getId()){ case R.id.bt_button: Intent intent=new Intent(MainActivity.this,TwoActivity.class); startActivity(intent); break; } } //(5)取消訊息訂閱 @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } } //--------------TwoActivity 中,用於傳送訊息------------------------ package eventbusdemo.wode.com.eventbusdemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; public class TwoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); Button bt_button2= (Button) findViewById(R.id.bt_button2); bt_button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //(3)產生事件,即傳送訊息 // EventBus.getDefault().post(Event.IMAGE_LOADER_SUCCESS); //傳值 EventBus.getDefault().post(new MessageEvent("哈哈哈哈哈")); } }); } }
相關推薦
Android中Intent傳值的幾種方法
1.使用putextra Intent intent = new Intent(); intent.putExtra("test","asdf"); intent.se
Android中的eventBus傳值
第一步:在build.gradle中新增依賴 dependencies { compile 'org.greenrobot:eventbus:3.0.0' } 第二步:建立一個 Event類: 注意:enum 不是class public enum Even
Android中WebView載入本地Html,與JavaScript與Android方法相互傳值
最近在做專案中,要使用HightChart來實現心電圖,於是,使用WebView載入本地html頁面,但是資料是通過藍芽裝置採集的資料,用Java程式碼獲取的資料,需要將資料傳到JavaScript中去,使用來繪製心電圖。以前都載入伺服器端返回的url地址,使用
60.android 簡單的EventBus傳值,簡單好用
//第一步 匯入依賴 implementation 'org.greenrobot:eventbus:3.0.0' //第二步 MainActivity裡傳送事件 //就寫了個簡單的Button按鈕,點選跳轉,傳值到Main2Activity pub
Android中WebView載入本地Html,與JavaScript與Android方法相互傳值(續)
接著上篇Android中WebView載入本地Html,與JavaScript與Android方法相互傳值,今天來一篇續集,為什麼要來續集呢,感覺自己還有沒有交代完的部分,於是在這裡再次交代一下,在Android中我們需要將給複雜的資料傳到Html頁面,我們該怎
C語言中的傳值調用
comment main 獲取 return line split -c data add 在c語言中每個變量都有兩個屬性一個是值,一個是址。比方: int a = 2; 變量a的值是2,變量a的地址,能夠用&取地址操作符獲取,
ASP.Net中頁面傳值的幾種方式
webconfig local 區別 重啟 Nid 傳遞對象 app too BE 大致概括一下,ASP.NET 頁面之間傳遞值得方式大致可以分為如下幾種:Request.QueryString["name"],Request.Form("name"),Session,Co
android webview JS傳值 prompt
near gravity should 使用 sre pen you eat fir 1. <!DOCTYPE html> <html style="min-height: 100%"> <head> <meta cha
Android中Settings預設值的設定
設定Settings的預設值有兩種方式 A. 在獲取是有一個介面可以進行預設值設定,當資料庫中查詢不到該值時,就會返回傳入的預設值。 public static int getInt(ContentResolver cr, String name, int def) {
Python函式中多型別傳值和冗餘引數及函式的遞迴呼叫
1.多型別傳值和冗餘引數 多型別傳值: def fun(x,y): return x +y print fun(3,5) 8 print fun(*t) 3 def fun(x,y,z): return x + y + z t1 = (1,2,3)
解決jsp中後臺傳值中文到前臺亂碼的問題
臨時要在一個jsp頁面使用到後臺傳過來的一個引數,想到中文可能會亂碼,所以一開始是這樣寫的: 1. 後臺java程式碼傳值過程: this.setName(URLEncoder.encode("測試","UTF-8")); 2.前臺jsp頁面取值過程,測試一下: <% S
Flask向模板中JS傳值簡便方式
後臺傳值: return render_template('statistics/numberofuserlogin/login_number.html', result_json = json.dumps(result)) 模板中JS取值: js_object = ev
Android中的顏色值
1、在android中經常看到設定的顏色為八位的十六進位制的顏色值,例如: 1 2 3
vue 元件之間使用eventbus傳值
對於前端的我們而言,並非是只有寫介面才是最大的問題,很多的情況下,我們需要關注的是資料,比如js頁面的資料傳遞等等,學習vue我們也是需要知道怎麼去使用資料 當然,使用儲存也是可以得,但是並非一定要快取,當然在vue中有推薦了我們去使用vuex去資料互動,Vuex會讓你的
Android中使用顏色值作為drawable
隨喜結佛緣 應用場景: 我們在Android中通常遇到這樣的情況,很多時候要給Button或者是RadioButton或
vue中props傳值
props是實現父元件傳遞訊息給子元件的,子元件不能通過這個傳值給父元件 用法如下: parent.vue(這是父元件檔案) <template> <div class="container"> <div>
點選recyclerview的item跳轉(eventbus)傳值
**第一步,匯入依賴,如: /*recyclerview依賴*/ compile 'com.android.support:recyclerview-v7:26.1.0' /*fresco載入圖片*/ compile 'com.f
android activity 相互傳值
傳遞單個簡單型別的情況:Main.activityPass value:Intent intent = newIntent(MainActivity.this,SecondActivity.class); intent.putExtra("Main","123"); star
【MVC】MVC中頁面傳值的幾種方式總結
原來寫程式碼的時候都是別人寫就照著寫,大部分都是返回json,使用js,但是對於其他的傳值方式用的少之又少,特別是當不使用easyUI的時候對它更是感覺到生疏,今天就對它的幾種傳值方式進行一下
MVC中url傳值和form傳值
客戶端的form 一般有兩個提交方式 為 GET 和 POST 不常用的有PUT 和DELETE <form action="/home/index?querystring_d=4" me