简体   繁体   English

单击一个选项卡中的按钮,然后在注释器选项卡中更改textview

[英]Click button in one tab and change textview in the anoter tab

I have tab1 and tab3 also these have their classes and I want to click button in tab1 and change textview in the tab3, but I couldn't find anyway. 我有tab1和tab3,也有它们的类,我想单击tab1中的按钮并更改tab3中的textview,但无论如何我都找不到。 This is my tab1 class 这是我的tab1课

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;
    }
}

This also my tab3 Class: 这也是我的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;
    }
}
 btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     //pager.setCurrentItem(yourindex);// if you use pager
     getTabHost().setCurrentTab(yourindex);

    }
});

If I'm reading your question correctly, then what you need is for tab3 to listen to events from tab1. 如果我正确地阅读了您的问题,那么您需要的是让tab3监听来自tab1的事件。 For that you will want to implement some kind of internal notification/eventing system. 为此,您将需要实现某种内部通知/事件系统。 This is typically handled through a notification handling class that will register observers/listeners. 这通常通过通知处理类进行处理,该类将注册观察者/侦听器。

An example from a project I've been maintaining: 我一直维护的项目的一个示例:

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);
    }
}

Then your tab3 class would need to implement the Observer interface and on object construction register itself with the NotificationManager with a string value for the type of notification it wants (use best practices for constants instead of string literal arguments), using the call 然后,您的tab3类将需要实现Observer接口,并在对象构造上使用要调用的通知类型向NotificationManager注册自己的字符串类型,以使用所需的通知类型(对常量使用最佳做法,而不是字符串文字参数)

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

It will need to implement the update(String, Bundle) method, which will make all the changes that you need. 它将需要实现update(String, Bundle)方法,该方法将进行所需的所有更改。 Then in the class for the tab1 object, add to the click listener this call: 然后在tab1对象的类中,将此调用添加到点击侦听器中:

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

Where data is any information that observers would need to know to respond. 数据是观察者需要响应的任何信息。 In keeping with the idea of decoupling code, do not put together a data bundle that is explicitly for one listener, because at some point you might need something else to listen for the same event. 与解耦代码的思想保持一致,不要将明确为一个侦听器指定的数据包放在一起,因为在某些时候,您可能还需要其他内容来侦听同一事件。 Save yourself some grief now by designing the data bundle to contain what would need to update regardless of who is consuming the event. 现在,通过设计数据包以包含无论谁消耗事件都需要更新的内容,可以为自己省去一些麻烦。

Some lessons learned for me: Pay attention to Android lifecycle. 我的一些经验教训:注意Android生命周期。 OnPause and OnDestroy for the active view(s) should unregister the listener so that you don't end up with a null pointer exception if something triggers that event while the observer object is not available. 活动视图的OnPause和OnDestroy应该注销侦听器,以便在观察者对象不可用时,如果某些事件触发了该事件,则不会导致空指针异常结束。 OnCreate and OnResume should reregister. OnCreate和OnResume应该重新注册。 In some cases I have been able to not worry about OnPause/OnResume, but depending on your app you may need them. 在某些情况下,我不必担心OnPause / OnResume,但是根据您的应用程序,您可能需要它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM