如何在一个活动中拥有多个按钮

我的Android应用程序有很多按钮。

我的main.xml布局有三个按钮。

我知道如何使用按钮从一个活动转到另一个活动,但我不知道如何在一个活动上有多个按钮,每个按钮启动的活动与另一个活动不同。

main.xml中

Button1 Button2

Main2.xml

由button1发起

About.xml

由Button2发布

我如何使main.java文件这样做?

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); } }); } } 

使用intent从一个活动移动到另一个活动。我们在按钮单击侦听器中编写该代码

这是一个相当广泛的问题,所以我的回答可能看起来同样广泛。

1.)首先要了解的是如何构建布局。 你说你已经有了3个按钮的布局。 在每个按钮的定义中,您需要指定一个android:id属性。 这将允许您稍后从您的Activity挂钩到该按钮。 有关更多信息,请参见此处

2.)一旦你用android:id定义你的3个按钮(为了便于讨论,让我们使用R.id.1,R.id.2和R.id.3)你想要将Java对象挂钩到这些元素在您的活动onCreate方法:

 Button button3 = (Button) getViewById(R.id.3) 

为3个按钮执行此操作。

3.)下一步是将onClick侦听器附加到按钮

 button3.setOnClickListener(new OnClickListener(){ public void onClick(View v){ //place code to execute here } }); 

与java中的大多数gui框架一样,此机制定义了单击按钮时执行的代码。 如果要从此按钮启动一个新的Activity,您将创建一个intent对象并启动它,如:

 Intent intent = new Intent(this, TheActivityClassYouWantToLaunch.class); startActivity(intent); 

将TheActivityClassYouWantToLaunch替换为扩展要启动的Activity的类的名称。 要了解更多信息[请查看Intents上的文档]( http://developer.android.com/reference/android/content/Intent.html

我知道这个问题早已得到解答,但是如果其他人偶然发现它,我想我会提供另一种方法来做我自己的代码使用的按钮是使用onClick和View。 onclick是在xml文件中完成的,只需按下你要点击的按钮并添加以下行:

  android:onClick="title" 

其中title是与该xml文件一起使用的java活动类文件部分的名称。 在java文件中,您将添加到公共类中:

 public void title(View buttonName) { // Should take user to the next view when button pressed Intent intent1 = new Intent(this, NextClass.class); startActivity(intent1); } 

其中buttonName是您添加onClick部分的按钮的名称,NextClass是您要访问的java文件的名称。 对于您的示例,在Main.xml中,您将转到button1并添加onClick代码,然后转到Main.java并添加public void代码,将标题更改为您想要的内容,例如launchMain2。 在同一个.java中,您可以将NextClass.class更改为Main2.class。

我使用它是因为我发现很容易复制并通过它,因为我有多个页面,我的应用程序的每个页面上只有几个按钮。 因此,对我来说,这有助于我节省在应用程序上工作的时间。

 i use this code ImageButton bot1; ImageButton bot2; ImageButton bot3; ImageButton bot4; Intent intentbot1; Intent intentbot2; Intent intentbot3; Intent intentbot4; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.currentactivity); bot1= (ImageButton)findViewById(R.id.bot1); bot2 = (ImageButton)findViewById(R.id.bot2); bot3 = (ImageButton)findViewById(R.id.bot3); bot4 = (ImageButton)findViewById(R.id.bot4); { final Intent intentbot1 = new Intent(); intentbot1.setClass(currentactivity.this, targetactivity.class); bot1.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(intentbot1); }});} { final Intent intentbot2 = new Intent(); intentbot2 .setClass(currentactivity.this, targetactivity.class); bot2.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(intentbot2 ); }});} { final Intent intentbot3 = new Intent(); intentbot3 .setClass(currentactivity.this, targetactivity.class); bot3.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(intentbot3 ); }}); } { final Intent intentbot4 = new Intent(); intentbot4 .setClass(currentactivity.this, targetactivity.class); bot4.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(intentbot4 ); }}); } }