简体   繁体   中英

How to display several layouts by clicking several buttons?

Files:

  • activity_main.xml (button1,button2)

  • layout1.xml (button3,button4,button5)

  • layout2.xml (button6,button7,button8)

  • layoutA.xml (multiple CheckBox and textView)

  • layoutB.xml (multiple CheckBox and textView)

  • layoutC.xml (multiple CheckBox and textView)

  • layoutD.xml (multiple CheckBox and textView)

  • layoutE.xml (multiple CheckBox and textView)

  • layoutF.xml (multiple CheckBox and textView)

Okay so what I want my app to do is:

Step 1 - First Screen activity_main.xml with 2 buttons

Step 2 - User clicks button1/button2 and layout1.xml/layout2.xml shows up.

Step 3 - User clicks any of the 3 buttons in any of the 2 layouts and accordingly layoutA/B/C/D/E/F with checkboxes and textview to show up.

Step 4 - User clicks Back button and instead of completely exiting the app, roll back only to the previous active layout like layoutA to layout1 , layout1 to activity_main ,etc.

I have been able to implement the first 2 steps but unable to do the other half.

I would be glad if someone could help me out. Thank You!

This is my MainActivity.java file

public class MainActivity extends Activity
{

    Button button1;
    Button button2;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                setContentView(layout1);
            }
        });
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                setContentView(R.layout.layout2);
            }
        });
    }
}

Start new Activity

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);

Start Activity with parameters

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("firstKeyName","FirstValue");
myIntent.putExtra("secondKeyName","SecondValue");
startActivity(myIntent);

Read data in started activity

Intent myIntent = getIntent(); // get previously intent
String firstKeyName = myIntent.getStringExtra("firstKeyName"); // this return "FirstValue"
String secondKeyName= myIntent.getStringExtra("secondKeyName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM