android: how to change layout on button click?

前端 未结 8 1347
醉话见心
醉话见心 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 :

    <activity android:name=".SecondActivity" ></activity>
    <activity android:name=".ThirdActivity" ></activity>
    

    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.

    0 讨论(0)
  • 2020-12-05 01:00

    I think what you're trying to do should be done with multiple Activities. If you're learning Android, understanding Activities is something you're going to have to tackle. Trying to write a whole app with just one Activity will end up being a lot more difficult. Read this article to get yourself started, then you should end up with something more like this:

    View.OnClickListener handler = new View.OnClickListener(){
        public void onClick(View v) {
    
            switch (v.getId()) {
    
                case R.id.DownloadView: 
                    // doStuff
                    startActivity(new Intent(ThisActivity.this, DownloadActivity.class));
                    break;
                case R.id.AppView: 
                    // doStuff
                    startActivity(new Intent(ThisActivity.this, AppActivity.class));
                    break;
            }
        }
    };
    
    findViewById(R.id.DownloadView).setOnClickListener(handler);
    findViewById(R.id.AppView).setOnClickListener(handler);
    
    0 讨论(0)
  • 2020-12-05 01:00

    First I would suggest putting a Log in each case of your switch to be sure that your code is being called.

    Then I would check that the layouts are actually different.

    0 讨论(0)
  • 2020-12-05 01:03

    The logcat shows the error, you should call super.onCreate(savedInstanceState) :

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //... your code
    }
    
    0 讨论(0)
  • 2020-12-05 01:03

    It is very simple, just do this:

    t4.setOnClickListener(new OnClickListener(){
    
            @Override
            public void onClick(View v) {
    
                launchQuiz2();          // TODO Auto-generated method stub
    
            }
    
            private void launchQuiz2() {
                Intent i = new Intent(MainActivity.this, Quiz2.class);
                startActivity(i);
                // TODO Auto-generated method stub
    
            }
    
        });
    
    0 讨论(0)
  • 2020-12-05 01:09

    I know I'm coming to this late, but what the heck.

    I've got almost the exact same code as Kris, using just one Activity but with 2 different layouts/views, and I want to switch between the layouts at will.

    As a test, I added 2 menu options, each one switches the view:

    public boolean onOptionsItemSelected(MenuItem item) {
    
            switch (item.getItemId()) {
                case R.id.item1:
                    setContentView(R.layout.main);
                    return true;
                case R.id.item2:
                    setContentView(R.layout.alternate);
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    

    Note, I've got one Activity class. This works perfectly. So I have no idea why people are suggesting using different Activities / Intents. Maybe someone can explain why my code works and Kris's didn't.

    0 讨论(0)
提交回复
热议问题