2017-05-20 39 views
0

我有tab1和tab3也有它們的類,我想單擊tab1中的按鈕並更改tab3中的textview,但我無法找到。 這是我TAB1類單擊一個選項卡中的按鈕並更改anoter選項卡中的textview

public class tab1Contacts extends Fragment{ 

    TextView tv; 
    EditText et; 
    TextView tv3; 
    personInfo pı; 

    public personInfo returnpı(){ 
     return pı; 
    } 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab1contents, container, false); 

     Button btn_jog = (Button) rootView.findViewById(R.id.jogging_button); 


     tv = (TextView) rootView.findViewById(R.id.newRecordText); 
     et = (EditText) rootView.findViewById(R.id.durationtext) ; 
     pı = new personInfo(); 
     pı.eyesPower = 100; 
     pı.brainPower = 100; 
     pı.armsPower = 100; 
     pı.legsPower = 100; 
     pı.hearthPower = 100; 
     pı.energyLevel = 100; 
     pı.calorie = 2000; 
     pı.condition = 0; 

      btn_jog.setOnClickListener(new View.OnClickListener() 
      { 
       @Override 
       public void onClick(View v) 
       { 
        int duration = Integer.parseInt(et.getText().toString()); 
        pı.jogging(duration); 
        //I want to change here textview in the tab3. 
       } 
      }); 
     return rootView; 
    } 
} 

這也是我TAB3類:

public class Tab3Contacts extends Fragment { 

    TextView tv3; 
    double newBrainpower; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab3contents, container, false); 
     tv3 = (TextView) rootView.findViewById(R.id.list_text) ; 


     return rootView; 
    } 
} 

回答

0
btn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    //pager.setCurrentItem(yourindex);// if you use pager 
    getTabHost().setCurrentTab(yourindex); 

    } 
}); 
+0

我無法解決getTabHost方法 –

+0

如果用戶單擊按鈕,我不想更改選項卡。我想在anoter選項卡中更改textview –

0

如果我正確地讀你的問題,那麼你需要的是爲TAB3收聽來自事件TAB1。爲此,您將需要實施某種內部通知/事件系統。這通常通過註冊觀察者/監聽者的通知處理類來處理。

從一個項目,我一直在維護一個例子:

public class NotificationManager { 
    public interface Observer { 
     public void update(String notificationName, Bundle data); 
    } 

    private static NotificationManager singletonNotifier = null; 
    private HashMap<String, ArrayList<Observer>> mObservables = null; 

    private NotificationManager() { 
     mObservables = new HashMap<String, ArrayList<Observer>>(); 
    } 

    //enforce singleton 
    public static NotificationManager getInstance() { 
     if (singletonNotifier == null) { 
      singletonNotifier = new NotificationManager(); 
     } 
     return singletonNotifier; 
    } 

    public void addObserver(String notificationName, Observer observer) { 
     // add to map 
     // in multi-threaded apps make sure you use synchronized or a mutex 
    } 

    public void removeObserver(String notificationName, Observer observer) { 
     // remove from map; mind threading 
     // overload as necessary for your design 
    } 

    public void notifyObservers(String notificationName, Bundle data) { 
     // go through your map of observers, build an array of observers 
     // that need to update, then for each observer, call 
     // observer.update(notificationName, data); 
    } 
} 

然後你TAB3類需要實現Observer接口和對象構造與針對該類型的字符串值NotificationManager註冊自己通知就是了(使用常量,而不是字符串文字形參的最佳實踐),使用呼叫

NotificationManager.getInstance().addObserver("Tab1DataChange", this); 

這將需要實現update(String, Bundle)方法,這將使所有的變化使y你需要。 然後在類的TAB1對象,添加到點擊收聽此調用:

NotificationManager.getInstance().notifyObservers("Tab1DataChange", data); 

其中數據的任何信息,觀察員將需要知道響應。爲了與解耦代碼的想法保持一致,不要將明確用於一個監聽器的數據捆綁在一起,因爲在某些時候,您可能需要其他的東西來監聽相同的事件。通過設計數據包來保存自己的一些悲傷,以包含無論誰在使用事件都需要更新的內容。

我吸取了一些教訓: 注意Android生命週期。活動視圖的OnPause和OnDestroy應取消註冊偵聽器,以便在觀察器對象不可用時觸發該事件的情況下不會導致空指針異常。 OnCreate和OnResume應該重新註冊。在某些情況下,我可以不用擔心OnPause/OnResume,但取決於您的應用程序,您可能需要它們。

相關問題