简体   繁体   中英

layout with multiplie buttons to diffrent layouts

I am trying to connect with a single button on each layout to a another layout. it just work from the activity to an another layout but when i try to connect the second layout with a 3rd one it wont work. I used the onCLick listener method with setContentView(); with Intent LayoutInflater. if i try to run the button with setContentView or Intent then it wont do anything and with the Layoutinflater the app will just crash. i hope you understand what i did (english is second language;) ) It will be nice if you tell me which method i need to use that i try it first by myself i don't want to show the code and you fix it for me:)

thx a lot

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1 = (Button) findViewById(R.id.button);
    Button b2 = (Button) findViewById(R.id.button_2);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.layout_2);
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.layout_2);

        }
    });

}

use below methods to switch from one intent to another

screen1 , button1 on button click use below code

startActivity(new Intent(screen1.this,screen2.class));

where screen2 is your class name of current activity and screen1 is the activity name where you want to jump

screen2 , button2 on button click use below code

startActivity(new Intent(screen2.this,screen1.class));

where screen1 is your class name of current activity and screen2 is the activity name where you want to jump

     use this on onclick change your activity name
     Intent intent = new Intent(MainActivity.this, secondActivity.class);
      startActivity(intent);

and second activity page use this on onclick change your activity name

 Intent intent = new Intent(secondActivity.this, thirdActivity.class);
      startActivity(intent);
    public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1 = (Button) findViewById(R.id.button);
    Button b2 = (Button) findViewById(R.id.button_2);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent inte_next=new Intent(MainActivity.this, Screen.class)
            startActivity(inte_next);
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent inte_next=new Intent(MainActivity.this, Screen2.class)
            startActivity(inte_next);

        }
    });

}




Screen.Java
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_2);
    }
}

使用片段代替

setContentView(R.layout.__);

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