android: how to change layout on button click?

前端 未结 8 1370
醉话见心
醉话见心 2020-12-05 00:35

I have to following code for selecting layout on button click.

View.OnClickListener handler = new View.OnClickListener(){
    public void onClick(View v) {

         


        
8条回答
  •  执念已碎
    2020-12-05 01:00

      Button btnDownload = (Button) findViewById(R.id.DownloadView);
      Button btnApp = (Button) findViewById(R.id.AppView);
    
      btnDownload.setOnClickListener(handler);
      btnApp.setOnClickListener(handler);
    
      View.OnClickListener handler = new View.OnClickListener(){
    
      public void onClick(View v) {
    
        if(v==btnDownload){ 
                // doStuff
                Intent intentMain = new Intent(CurrentActivity.this , 
                                               SecondActivity.class);
                CurrentActivity.this.startActivity(intentMain);
                Log.i("Content "," Main layout ");
        }
    
        if(v==btnApp){ 
                // doStuff
                Intent intentApp = new Intent(CurrentActivity.this, 
                                              ThirdActivity.class);
    
                CurrentActivity.this.startActivity(intentApp);
    
                Log.i("Content "," App layout ");
    
        }
       }
      };
    

    Note : and then you should declare all your activities in the manifest .xml file like this :

    
    
    

    EDIT : update this part of Code :) :

    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);// Add THIS LINE
    
        setContentView(R.layout.app);
    
        TextView tv = (TextView) this.findViewById(R.id.thetext);
        tv.setText("App View yo!?\n");
    }
    

    NB : check this (Broken link) Tutorial About How To Switch Between Activities.

提交回复
热议问题