【问题标题】:Click button in one tab and change textview in the anoter tab单击一个选项卡中的按钮并在另一个选项卡中更改文本视图
【发布时间】:2017-05-20 19:02:16
【问题描述】:

我有 tab1 和 tab3 也有它们的类,我想单击 tab1 中的按钮并更改 tab3 中的 textview,但我还是找不到。 这是我的 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;
    }
}

这也是我的 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;
    }
}

【问题讨论】:

    标签: java android android-fragments android-tabs


    【解决方案1】:
     btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //pager.setCurrentItem(yourindex);// if you use pager
         getTabHost().setCurrentTab(yourindex);
    
        }
    });
    

    【讨论】:

    • 如果用户单击按钮,我不想更改选项卡。我想在另一个选项卡中更改文本视图
    【解决方案2】:

    如果我正确阅读了您的问题,那么您需要让 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) 方法,该方法将进行您需要的所有更改。 然后在 tab1 对象的类中,将这个调用添加到点击监听器中:

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

    其中数据是观察者需要知道才能做出响应的任何信息。与解耦代码的想法保持一致,不要将明确用于一个侦听器的数据包放在一起,因为在某些时候您可能需要其他东西来侦听同一事件。现在,通过设计数据包以包含无论谁在消费事件都需要更新的内容,从而为自己省去一些麻烦。

    我学到的一些经验教训: 注意 Android 生命周期。活动视图的 OnPause 和 OnDestroy 应取消注册侦听器,以便在观察者对象不可用时触发该事件时不会出现空指针异常。 OnCreate 和 OnResume 应该重新注册。在某些情况下,我可以不用担心 OnPause/OnResume,但根据您的应用,您可能需要它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多