4月 13, 2018

『Android』將Activity的值傳給View的方法

Activity在Android中,常常被認定是一種容器

可以在裡面塞入很多View或是Fragment

但是View跟Activity該如何傳值給雙方知道呢?

1. 在你的View Class裡面創一個Bundle
Bundle mViewBundle = new Bundle();

2. 並在你的View Class裡面加入一個方法(以傳字串為例)
public void setData(Bundle data){
    mViewBundle = data;
    Log.i("View","String : " + mViewBundle.getString("key"));
}

3. 在你的Activity Class裡面就可以在initial View之後隨時去設定值給View
Bundle mActivityBundle = new Bundle();
View mView = new View(MainActivity.this);

mActivityBundle.putString("Key","Value");
mView.setData(mActivityBundle);



1 則留言:

  1. Baccarat | Free to play games - Viking - Gruppe
    The game choegocasino is played 인카지노 with four decks of playing cards: a spade or spade. The cards worrione are shuffled and then dealt out in a regular 52 card game

    回覆刪除

Related Posts Plugin for WordPress, Blogger...