简体   繁体   中英

I wrote this few lines of code to change the xml file of this class but it doesn't work

Im a Beginner in Java and Android Studio. With this Code I try to change the layout in this activity. The current layout is "marcelscorpion_1". Only the Buttons "weiter_1" and "zurück_1" are working and I don't know why...

public void SwitchLayout()
{
    Button weiter_1 = (Button) findViewById(R.id.marcelscorpion_weiter1);
    Button zurück_1 = (Button) findViewById(R.id.marcelscorpion_zurück1);

    View marcelscorpion_2 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_2, null);
    Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
    Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);

    View marcelscorpion_3 = LayoutInflater.from(getApplication()).inflate(R.layout.marcelscorpion_3, null);
    Button weiter_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_weiter3);
    Button zurück_3 = (Button) marcelscorpion_3.findViewById(R.id.marcelscorpion_zurück3);



    weiter_1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.marcelscorpion_2);
        }
    });

    weiter_2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.marcelscorpion_3);
        }
    });

    weiter_3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.marcelscorpion_1);
        }
    });

    zurück_1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.marcelscorpion_3);
        }
    });

    zurück_2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.marcelscorpion_1);
        }
    });

    zurück_3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.marcelscorpion_2);
        }
    });
}

Need Help ;) Thank you!

Every time you call setContentView() you should find and setOnClickListener(..) again to all buttons contained in the new layout.

A better way is define onclick attribute in the layouts xml file. For instance if you have this:

<Button
    android:id="@+id/w2"
    android:onClick="getms3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="w2" />

then you should define

public void getms3(View v) 
{
   setContentView(R.layout.ms3);
}

Just add your buttons of that layout when you are doing setContentView() in onClick() methods like this :

weiter_1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.marcelscorpion_2);
            Button weiter_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_weiter2);
Button zurück_2 = (Button) marcelscorpion_2.findViewById(R.id.marcelscorpion_zurück2);

        }
    });

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