Button Listener for button in fragment in android

后端 未结 9 2120
北恋
北恋 2020-12-02 11:04

I am new to Android and trying to learn on my own. But I am having a tough time with Fragments. I am creating a simple application to learn fragments. I think it may seem si

9条回答
  •  情书的邮戳
    2020-12-02 11:05

        //sure run it i will also test it
    //we make a class that extends with the fragment
        public class Example_3_1 extends Fragment  implements OnClickListener
        {
           View vi;
            EditText t;
            EditText t1;
            Button bu;
     // that are by defult function of fragment extend class
         @Override
           public View onCreateView(LayoutInflater inflater,ViewGroup container,BundlesavedInstanceState) 
           {        
               vi=inflater.inflate(R.layout.example_3_1, container, false);// load the xml file 
               bu=(Button) vi.findViewById(R.id.button1);// get button id from example_3_1 xml file
               bu.setOnClickListener(this); //on button appay click listner
               t=(EditText) vi.findViewById(R.id.editText1);// id get from example_3_1 xml file
               t1=(EditText) vi.findViewById(R.id.editText2);
              return vi; // return the view object,that set the xml file  example_3_1 xml file
           }
           @Override
           public void onClick(View v)//on button click that called
           {
    
              switch(v.getId())// on run time get id what button os click and get id
              {
              case R.id.button1:        // it mean if button1 click then this work
               t.setText("UMTien");     //set text 
               t1.setText("programming");
               break;
              }
        }     }
    

提交回复
热议问题