android: how to change layout on button click?

前端 未结 8 1376
醉话见心
醉话见心 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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);
    

提交回复
热议问题