How to set Different String in a Textview in Fragment while clicking different Button?

后端 未结 2 1000
执念已碎
执念已碎 2020-12-22 08:50

I\'m new to Android app programming.

I have two Fragments - fragmentA.xml and fragmentB.xml.

In fragmentA there are tw

2条回答
  •  情歌与酒
    2020-12-22 09:40

    FragmentA.java

    public boolean btn1Clicked, btn2Clicked = false;
            Button button1 = (Button) rootViewfindViewById(R.id.button1);
                button1.setOnClickListener(new View.OnClickListener() {
    
                  @Override
                  public void onClick(View v) {
                    btn1Clicked = true;
                  }
                });
    
                Button button2 = (Button) rootView.findViewById(R.id.button2);
                button2.setOnClickListener(new View.OnClickListener() {
    
                  @Override
                  public void onClick(View v) {
                    btn2clicked = true;
                  }
                });
    

    FragmentB.java

     public View onCreateView(
            LayoutInflater inflater, 
            ViewGroup container, 
            Bundle savedInstanceState) 
        {
            View rootView = inflater.inflate(R.layout.oop_page,container,false);
            TextView tv1 = (TextView)rootView.findViewById(R.id.textView);
            if(!(FragmentA.btn1Clicked))
             {
            // Not Clicked
             }
            else{
               tv1.setText("Button 1 Clicked!")'
        }
            return rootView;
        }
    

提交回复
热议问题