Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

java - How to have multiple buttons in one activity

My Android app having many buttons.

My main.xml layout has three buttons.

I know how to use buttons to go from one activity to another, But i don't know how to have multiple buttons on one activity, with each launching a different activity than the other.

EXAMPLE

Main.xml

Button1 Button2

Main2.xml

Launched by button1

About.xml

Launched by Button2

How do i make the main.java file do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
public class NL extends Activity {



     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
          Button b1=(Button)findViewById(R.id.Button01);
          Button b2=(Button)findViewById(R.id.Button02);
          b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent myintent2 = new Intent(NL.this,Button1.class);
                startActivity(myintent2);

            }
        });
          b2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    Intent myintent2 = new Intent(NL.this,Button2.class);
                    startActivity(myintent2);

                }
            }); 
    }
}

move from one activity to another activity using intent.we write that code in button click listener


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...